aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-13 13:18:05 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-13 13:18:05 +0800
commit8558f80b4077536d6aa46b97328d6df1210e261f (patch)
tree373a2242db00ddeb9a1af83dbd853e97e0bd12d3
parent65321c12beb31c5743e0cf3f28e7aec178b730a2 (diff)
downloadrcc-project-8558f80b4077536d6aa46b97328d6df1210e261f.tar.gz
rcc-project-8558f80b4077536d6aa46b97328d6df1210e261f.tar.xz
[Support] Centralize error context handling
-rw-r--r--backend/include/rcc/Support/Error.h2
-rw-r--r--backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp31
-rw-r--r--backend/lib/CodeGen/GlobalISel/Legalizer.cpp17
-rw-r--r--backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp27
-rw-r--r--backend/lib/CodeGen/MachineBlockPlacement.cpp9
-rw-r--r--backend/lib/CodeGen/MachineBranchOptimization.cpp9
-rw-r--r--backend/lib/CodeGen/MachineBranchRelaxation.cpp9
-rw-r--r--backend/lib/CodeGen/MachineCSE.cpp10
-rw-r--r--backend/lib/CodeGen/MachineCombiner.cpp9
-rw-r--r--backend/lib/CodeGen/MachineCopyPropagation.cpp9
-rw-r--r--backend/lib/CodeGen/MachineDCE.cpp10
-rw-r--r--backend/lib/CodeGen/MachineFallthrough.cpp11
-rw-r--r--backend/lib/CodeGen/MachineLICM.cpp10
-rw-r--r--backend/lib/CodeGen/MachineMemoryFolding.cpp9
-rw-r--r--backend/lib/CodeGen/MachinePassManager.cpp41
-rw-r--r--backend/lib/CodeGen/MachinePseudoExpansion.cpp15
-rw-r--r--backend/lib/CodeGen/MachineScheduler.cpp9
-rw-r--r--backend/lib/CodeGen/MachineSink.cpp10
-rw-r--r--backend/lib/CodeGen/MachineStackSlotColoring.cpp9
-rw-r--r--backend/lib/CodeGen/MachineTailDuplication.cpp9
-rw-r--r--backend/lib/CodeGen/MachineTailMerge.cpp10
-rw-r--r--backend/lib/CodeGen/PHIElimination.cpp13
-rw-r--r--backend/lib/CodeGen/PostRAScheduler.cpp9
-rw-r--r--backend/lib/CodeGen/PrologueEpilogueInsertion.cpp22
-rw-r--r--backend/lib/CodeGen/RegAllocBasic.cpp2
-rw-r--r--backend/lib/CodeGen/RegAllocFast.cpp2
-rw-r--r--backend/lib/CodeGen/RegAllocInternal.h9
-rw-r--r--backend/lib/CodeGen/RegAllocPBQP.cpp2
-rw-r--r--backend/lib/CodeGen/TwoAddressInstructionPass.cpp14
-rw-r--r--backend/lib/Support/Error.cpp5
30 files changed, 119 insertions, 234 deletions
diff --git a/backend/include/rcc/Support/Error.h b/backend/include/rcc/Support/Error.h
index 0429cc0..b631635 100644
--- a/backend/include/rcc/Support/Error.h
+++ b/backend/include/rcc/Support/Error.h
@@ -56,6 +56,8 @@ private:
std::string Message;
};
+[[nodiscard]] Error withErrorContext(const Error &Failure, std::string Context);
+
template <typename ValueT> using Expected = std::expected<ValueT, Error>;
[[nodiscard]] inline std::unexpected<Error> makeUnexpected(ErrorCode Code, std::string Message) {
diff --git a/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp b/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp
index 344e63e..c001ac5 100644
--- a/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp
+++ b/backend/lib/CodeGen/GlobalISel/InstructionSelect.cpp
@@ -167,11 +167,6 @@ void combineFingerprint(std::uint64_t &State, std::uint64_t Value) noexcept {
return Error(Code, std::move(Message));
}
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
[[nodiscard]] Expected<void> repairTrackedLiveness(MachineFunction &Function,
const TargetDescription &Target,
MachinePassResult &Result) {
@@ -207,7 +202,7 @@ Expected<MachinePassResult> InstructionSelect::run(MachineFunction &Function,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "instruction-selection transaction setup failed: "));
+ withErrorContext(Trial.error(), "instruction-selection transaction setup failed: "));
}
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
@@ -216,9 +211,9 @@ Expected<MachinePassResult> InstructionSelect::run(MachineFunction &Function,
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(
- prefixedError(Result.error(),
- "instruction selection diverged after a successful transaction preflight: "));
+ return std::unexpected(withErrorContext(
+ Result.error(),
+ "instruction selection diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
if (!Replayed)
@@ -242,7 +237,7 @@ Expected<MachinePassResult> InstructionSelect::runImplementation(MachineFunction
auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
if (!Validation)
return std::unexpected(
- prefixedError(Validation.error(), "instruction selector input is invalid: "));
+ withErrorContext(Validation.error(), "instruction selector input is invalid: "));
if (!Descriptor->Flags.has(InstructionFlag::Generic))
continue;
auto Match = Info->canSelect(*Instruction, Function, Target);
@@ -291,13 +286,13 @@ Expected<MachinePassResult> InstructionSelect::runImplementation(MachineFunction
passFailure("instruction selector produced an unknown instruction descriptor"));
auto Constraint = Info->constrainSelectedInstruction(*Instruction, Function, Target);
if (!Constraint) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Constraint.error(), "instruction selector could not constrain selected registers: "));
}
auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
if (!Validation)
- return std::unexpected(
- prefixedError(Validation.error(), "instruction selector produced invalid MachineIR: "));
+ return std::unexpected(withErrorContext(Validation.error(),
+ "instruction selector produced invalid MachineIR: "));
const OpcodeState After = stateOf(*Instruction);
if (After == Before)
return std::unexpected(
@@ -330,20 +325,20 @@ Expected<MachinePassResult> InstructionSelect::runImplementation(MachineFunction
passFailure("instruction selector produced an unknown instruction descriptor"));
auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
if (!Validation)
- return std::unexpected(
- prefixedError(Validation.error(), "instruction selector produced invalid MachineIR: "));
+ return std::unexpected(withErrorContext(Validation.error(),
+ "instruction selector produced invalid MachineIR: "));
if (Descriptor->Flags.has(InstructionFlag::Generic))
return std::unexpected(
passFailure("instruction selector left a generic opcode in the function"));
}
auto RegisterValidation = Target.validateRegisterInfo(Function.registerInfo());
if (!RegisterValidation)
- return std::unexpected(prefixedError(RegisterValidation.error(),
- "instruction selection produced invalid registers: "));
+ return std::unexpected(withErrorContext(RegisterValidation.error(),
+ "instruction selection produced invalid registers: "));
auto Liveness = repairTrackedLiveness(Function, Target, Result);
if (!Liveness) {
return std::unexpected(
- prefixedError(Liveness.error(), "instruction-selection liveness repair failed: "));
+ withErrorContext(Liveness.error(), "instruction-selection liveness repair failed: "));
}
Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
return Result;
diff --git a/backend/lib/CodeGen/GlobalISel/Legalizer.cpp b/backend/lib/CodeGen/GlobalISel/Legalizer.cpp
index 7781818..83efb6a 100644
--- a/backend/lib/CodeGen/GlobalISel/Legalizer.cpp
+++ b/backend/lib/CodeGen/GlobalISel/Legalizer.cpp
@@ -106,11 +106,6 @@ void combineFingerprint(std::uint64_t &State, std::uint64_t Value) noexcept {
return Error(Code, std::move(Message));
}
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
[[nodiscard]] std::string_view legalizationActionName(LegalizationAction Action) noexcept {
switch (Action) {
case LegalizationAction::Legal:
@@ -187,7 +182,7 @@ Expected<MachinePassResult> Legalizer::run(MachineFunction &Function,
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "legalizer transaction setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "legalizer transaction setup failed: "));
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
if (!TrialResult)
@@ -195,7 +190,7 @@ Expected<MachinePassResult> Legalizer::run(MachineFunction &Function,
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "legalizer diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
@@ -231,7 +226,7 @@ Expected<MachinePassResult> Legalizer::runImplementation(MachineFunction &Functi
auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
if (!Validation)
- return std::unexpected(prefixedError(Validation.error(), "legalizer input is invalid: "));
+ return std::unexpected(withErrorContext(Validation.error(), "legalizer input is invalid: "));
auto Action = Info->classify(*Instruction, Function, Target);
if (!Action)
@@ -253,7 +248,7 @@ Expected<MachinePassResult> Legalizer::runImplementation(MachineFunction &Functi
auto LoweringValidation = Info->validateLowering(*Instruction, Function, Target, *Action);
if (!LoweringValidation)
return std::unexpected(
- prefixedError(LoweringValidation.error(), "legalizer lowering precondition failed: "));
+ withErrorContext(LoweringValidation.error(), "legalizer lowering precondition failed: "));
std::string Rule = "legalize.";
Rule.append(legalizationActionName(*Action));
Rule.push_back('.');
@@ -311,12 +306,12 @@ Expected<MachinePassResult> Legalizer::runImplementation(MachineFunction &Functi
auto FinalValidation = Target.validateInstruction(*Instruction, Function.registerInfo());
if (!FinalValidation)
return std::unexpected(
- prefixedError(FinalValidation.error(), "legalizer produced invalid MachineIR: "));
+ withErrorContext(FinalValidation.error(), "legalizer produced invalid MachineIR: "));
}
auto Liveness = clearTrackedLivenessFlags(Function, Result);
if (!Liveness)
return std::unexpected(
- prefixedError(Liveness.error(), "legalizer liveness-flag invalidation failed: "));
+ withErrorContext(Liveness.error(), "legalizer liveness-flag invalidation failed: "));
Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
return Result;
}
diff --git a/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp b/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp
index 041ab74..22c80f7 100644
--- a/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp
+++ b/backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp
@@ -95,11 +95,6 @@ registerConfigurationFingerprint(const MachineRegisterInfo &RegInfo) noexcept {
return Error(Code, std::move(Message));
}
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
[[nodiscard]] Expected<void> repairTrackedLiveness(MachineFunction &Function,
const TargetDescription &Target,
MachinePassResult &Result) {
@@ -173,7 +168,7 @@ Expected<MachinePassResult> RegBankSelect::run(MachineFunction &Function,
auto Validation = Info->validateInstruction(*Instruction, Function, Target);
if (!Validation)
return std::unexpected(
- prefixedError(Validation.error(), "register-bank mapping precondition failed: "));
+ withErrorContext(Validation.error(), "register-bank mapping precondition failed: "));
}
for (const auto &[Reg, Bank] : PlannedBanks) {
@@ -197,25 +192,27 @@ Expected<MachinePassResult> RegBankSelect::run(MachineFunction &Function,
if (!Constraint) {
auto Restored = Restore();
if (!Restored)
- return std::unexpected(prefixedError(Restored.error(), "register-bank rollback failed: "));
+ return std::unexpected(
+ withErrorContext(Restored.error(), "register-bank rollback failed: "));
return std::unexpected(Constraint.error());
}
auto Validation = Target.validateInstruction(*Instruction, Function.registerInfo());
if (!Validation) {
auto Restored = Restore();
if (!Restored)
- return std::unexpected(prefixedError(Restored.error(), "register-bank rollback failed: "));
- return std::unexpected(prefixedError(Validation.error(),
- "register-bank selection produced invalid MachineIR: "));
+ return std::unexpected(
+ withErrorContext(Restored.error(), "register-bank rollback failed: "));
+ return std::unexpected(withErrorContext(
+ Validation.error(), "register-bank selection produced invalid MachineIR: "));
}
}
auto RegisterValidation = Target.validateRegisterInfo(RegInfo);
if (!RegisterValidation) {
auto Restored = Restore();
if (!Restored)
- return std::unexpected(prefixedError(Restored.error(), "register-bank rollback failed: "));
- return std::unexpected(prefixedError(RegisterValidation.error(),
- "register-bank selection produced invalid registers: "));
+ return std::unexpected(withErrorContext(Restored.error(), "register-bank rollback failed: "));
+ return std::unexpected(withErrorContext(
+ RegisterValidation.error(), "register-bank selection produced invalid registers: "));
}
if (registerConfigurationFingerprint(RegInfo) != RegisterConfigurationBefore) {
@@ -227,9 +224,9 @@ Expected<MachinePassResult> RegBankSelect::run(MachineFunction &Function,
if (!Liveness) {
auto Restored = Restore();
if (!Restored)
- return std::unexpected(prefixedError(Restored.error(), "register-bank rollback failed: "));
+ return std::unexpected(withErrorContext(Restored.error(), "register-bank rollback failed: "));
return std::unexpected(
- prefixedError(Liveness.error(), "register-bank liveness repair failed: "));
+ withErrorContext(Liveness.error(), "register-bank liveness repair failed: "));
}
Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
return Result;
diff --git a/backend/lib/CodeGen/MachineBlockPlacement.cpp b/backend/lib/CodeGen/MachineBlockPlacement.cpp
index 1e563e1..8e2242c 100644
--- a/backend/lib/CodeGen/MachineBlockPlacement.cpp
+++ b/backend/lib/CodeGen/MachineBlockPlacement.cpp
@@ -51,11 +51,6 @@ enum class PlacementClass : std::uint8_t {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] PlacementClass classify(const MachineBasicBlock &Block,
std::uint64_t Frequency) noexcept {
const MachineBasicBlockAttributes &Attributes = Block.attributes();
@@ -112,7 +107,7 @@ MachineBlockPlacementPass::run(MachineFunction &Function, const TargetDescriptio
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "block placement setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "block placement setup failed: "));
MachineFunctionAnalysisManager TrialAnalyses;
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses, Actions);
@@ -121,7 +116,7 @@ MachineBlockPlacementPass::run(MachineFunction &Function, const TargetDescriptio
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Analyses, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "block placement diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/MachineBranchOptimization.cpp b/backend/lib/CodeGen/MachineBranchOptimization.cpp
index c102931..d09de97 100644
--- a/backend/lib/CodeGen/MachineBranchOptimization.cpp
+++ b/backend/lib/CodeGen/MachineBranchOptimization.cpp
@@ -45,11 +45,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -311,14 +306,14 @@ Expected<MachinePassResult> MachineBranchOptimizationPass::run(MachineFunction &
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "branch optimization transaction setup failed: "));
+ withErrorContext(Trial.error(), "branch optimization transaction setup failed: "));
}
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
if (!TrialResult)
return std::unexpected(TrialResult.error());
auto Result = runImplementation(Function, Target);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "branch optimization diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineBranchRelaxation.cpp b/backend/lib/CodeGen/MachineBranchRelaxation.cpp
index c11ed8e..9f14fc4 100644
--- a/backend/lib/CodeGen/MachineBranchRelaxation.cpp
+++ b/backend/lib/CodeGen/MachineBranchRelaxation.cpp
@@ -47,11 +47,6 @@ struct MachineLayout final {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] Expected<std::uint64_t> alignTo(std::uint64_t Value, std::uint64_t Alignment) {
if (Alignment == 0 || (Alignment & (Alignment - 1U)) != 0)
return makeUnexpected(ErrorCode::VerificationError, "block alignment is not a power of two");
@@ -118,7 +113,7 @@ MachineBranchRelaxationPass::run(MachineFunction &Function, const TargetDescript
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "branch relaxation setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "branch relaxation setup failed: "));
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
if (!TrialResult)
@@ -126,7 +121,7 @@ MachineBranchRelaxationPass::run(MachineFunction &Function, const TargetDescript
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "branch relaxation diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/MachineCSE.cpp b/backend/lib/CodeGen/MachineCSE.cpp
index 92fe1bd..52a1484 100644
--- a/backend/lib/CodeGen/MachineCSE.cpp
+++ b/backend/lib/CodeGen/MachineCSE.cpp
@@ -44,11 +44,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -323,7 +318,8 @@ Expected<MachinePassResult> MachineCSEPass::run(MachineFunction &Function,
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
- return std::unexpected(prefixedError(Trial.error(), "machine CSE transaction setup failed: "));
+ return std::unexpected(
+ withErrorContext(Trial.error(), "machine CSE transaction setup failed: "));
}
MachineFunctionAnalysisManager TrialAnalyses;
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
@@ -332,7 +328,7 @@ Expected<MachinePassResult> MachineCSEPass::run(MachineFunction &Function,
auto Result = runImplementation(Function, Target, Analyses);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "machine CSE diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineCombiner.cpp b/backend/lib/CodeGen/MachineCombiner.cpp
index c3e9795..d921647 100644
--- a/backend/lib/CodeGen/MachineCombiner.cpp
+++ b/backend/lib/CodeGen/MachineCombiner.cpp
@@ -113,11 +113,6 @@ namespace {
return PreLegalizer;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.flags().has(MachineInstrFlag::FrameSetup) ||
Instruction.flags().has(MachineInstrFlag::FrameDestroy) ||
@@ -305,7 +300,7 @@ Expected<MachinePassResult> MachineCombinerPass::run(MachineFunction &Function,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "machine combiner transaction setup failed: "));
+ withErrorContext(Trial.error(), "machine combiner transaction setup failed: "));
}
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
@@ -314,7 +309,7 @@ Expected<MachinePassResult> MachineCombinerPass::run(MachineFunction &Function,
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "machine combiner diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/MachineCopyPropagation.cpp b/backend/lib/CodeGen/MachineCopyPropagation.cpp
index 9725399..2c4cc44 100644
--- a/backend/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/backend/lib/CodeGen/MachineCopyPropagation.cpp
@@ -62,11 +62,6 @@ copyPropagationContract(MachineCopyPropagationStage Stage) {
return Stage == MachineCopyPropagationStage::PreRegisterAllocation ? PreRA : PostRA;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -302,7 +297,7 @@ MachineCopyPropagationPass::run(MachineFunction &Function, const TargetDescripti
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "COPY propagation transaction setup failed: "));
+ withErrorContext(Trial.error(), "COPY propagation transaction setup failed: "));
}
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
@@ -311,7 +306,7 @@ MachineCopyPropagationPass::run(MachineFunction &Function, const TargetDescripti
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "COPY propagation diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/MachineDCE.cpp b/backend/lib/CodeGen/MachineDCE.cpp
index 1c53c31..87c15d9 100644
--- a/backend/lib/CodeGen/MachineDCE.cpp
+++ b/backend/lib/CodeGen/MachineDCE.cpp
@@ -38,11 +38,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.flags().has(MachineInstrFlag::FrameSetup) ||
Instruction.flags().has(MachineInstrFlag::FrameDestroy) ||
@@ -125,7 +120,8 @@ Expected<MachinePassResult> MachineDCEPass::run(MachineFunction &Function,
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
- return std::unexpected(prefixedError(Trial.error(), "machine DCE transaction setup failed: "));
+ return std::unexpected(
+ withErrorContext(Trial.error(), "machine DCE transaction setup failed: "));
}
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
if (!TrialResult)
@@ -133,7 +129,7 @@ Expected<MachinePassResult> MachineDCEPass::run(MachineFunction &Function,
auto Result = runImplementation(Function, Target);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "machine DCE diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineFallthrough.cpp b/backend/lib/CodeGen/MachineFallthrough.cpp
index 608b703..3619155 100644
--- a/backend/lib/CodeGen/MachineFallthrough.cpp
+++ b/backend/lib/CodeGen/MachineFallthrough.cpp
@@ -36,11 +36,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] Expected<std::vector<MachineBasicBlock *>>
normalizeFallthroughs(MachineFunction &Function, const TargetDescription &Target,
const TargetInstrInfo &Info) {
@@ -129,7 +124,7 @@ Expected<MachinePassResult> MachineFallthroughPass::run(MachineFunction &Functio
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "fallthrough setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "fallthrough setup failed: "));
MachineFunctionAnalysisManager TrialAnalyses;
auto TrialLiveness =
TrialAnalyses.getResult<LiveVariablesAnalysis>(*Trial->ClonedFunction, Target);
@@ -147,8 +142,8 @@ Expected<MachinePassResult> MachineFallthroughPass::run(MachineFunction &Functio
return std::unexpected(Liveness.error());
auto ChangedBlocks = normalizeFallthroughs(Function, Target, *Info);
if (!ChangedBlocks) {
- return std::unexpected(prefixedError(ChangedBlocks.error(),
- "fallthrough diverged after a successful preflight: "));
+ return std::unexpected(withErrorContext(ChangedBlocks.error(),
+ "fallthrough diverged after a successful preflight: "));
}
auto Repaired = repairPostRAMachineBlockLiveness(*ChangedBlocks, Target, **Liveness);
if (!Repaired)
diff --git a/backend/lib/CodeGen/MachineLICM.cpp b/backend/lib/CodeGen/MachineLICM.cpp
index 31d792b..8a34d3b 100644
--- a/backend/lib/CodeGen/MachineLICM.cpp
+++ b/backend/lib/CodeGen/MachineLICM.cpp
@@ -62,11 +62,6 @@ namespace {
return Stage == MachineCodeMotionStage::SelectedSSA ? Selected : PostRA;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool containsBlock(const MachineLoop &Loop, const MachineBasicBlock *Block) noexcept {
return std::ranges::find(Loop.Blocks, Block) != Loop.Blocks.end();
}
@@ -344,7 +339,8 @@ Expected<MachinePassResult> MachineLICMPass::run(MachineFunction &Function,
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
- return std::unexpected(prefixedError(Trial.error(), "machine LICM transaction setup failed: "));
+ return std::unexpected(
+ withErrorContext(Trial.error(), "machine LICM transaction setup failed: "));
}
MachineFunctionAnalysisManager TrialAnalyses;
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
@@ -353,7 +349,7 @@ Expected<MachinePassResult> MachineLICMPass::run(MachineFunction &Function,
auto Result = runImplementation(Function, Target, Analyses);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "machine LICM diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineMemoryFolding.cpp b/backend/lib/CodeGen/MachineMemoryFolding.cpp
index e3d796a..75200f8 100644
--- a/backend/lib/CodeGen/MachineMemoryFolding.cpp
+++ b/backend/lib/CodeGen/MachineMemoryFolding.cpp
@@ -42,11 +42,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -195,14 +190,14 @@ Expected<MachinePassResult> MachineMemoryFoldingPass::run(MachineFunction &Funct
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "memory folding transaction setup failed: "));
+ withErrorContext(Trial.error(), "memory folding transaction setup failed: "));
}
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
if (!TrialResult)
return std::unexpected(TrialResult.error());
auto Result = runImplementation(Function, Target);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "memory folding diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachinePassManager.cpp b/backend/lib/CodeGen/MachinePassManager.cpp
index 0810965..2cd81b9 100644
--- a/backend/lib/CodeGen/MachinePassManager.cpp
+++ b/backend/lib/CodeGen/MachinePassManager.cpp
@@ -44,11 +44,6 @@ using Clock = std::chrono::steady_clock;
return static_cast<std::uint64_t>(Elapsed.count());
}
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasDuplicateAnalysisIDs(std::span<const AnalysisID> IDs) {
std::unordered_set<AnalysisID> Unique;
for (const AnalysisID ID : IDs) {
@@ -786,8 +781,8 @@ PassManagerCallbacks<MachineFunction, MachineFunctionPass, MachinePassManagerOpt
if (Options.VerifyBefore) {
auto Verification = MachineVerifier::verify(Function, Target);
if (!Verification) {
- Error Failure = prefixedError(Verification.error(), "pass '" + std::string(Name) +
- "' received invalid MachineIR: ");
+ Error Failure = withErrorContext(
+ Verification.error(), "pass '" + std::string(Name) + "' received invalid MachineIR: ");
if (Instrumentation != nullptr)
Instrumentation->failure(Name, Function, Failure);
Analyses.setInstrumentation(nullptr);
@@ -799,8 +794,8 @@ PassManagerCallbacks<MachineFunction, MachineFunctionPass, MachinePassManagerOpt
auto Result = Analyses.getResultByID(Required, Function, Target);
if (!Result) {
Error Failure =
- prefixedError(Result.error(), "pass '" + std::string(Name) +
- "' could not materialize a required analysis: ");
+ withErrorContext(Result.error(), "pass '" + std::string(Name) +
+ "' could not materialize a required analysis: ");
if (Instrumentation != nullptr)
Instrumentation->failure(Name, Function, Failure);
Analyses.setInstrumentation(nullptr);
@@ -819,7 +814,7 @@ PassManagerCallbacks<MachineFunction, MachineFunctionPass, MachinePassManagerOpt
const std::uint64_t Duration = elapsedNanoseconds(Start);
if (!PassResult) {
Error Failure =
- prefixedError(PassResult.error(), "pass '" + std::string(Name) + "' failed: ");
+ withErrorContext(PassResult.error(), "pass '" + std::string(Name) + "' failed: ");
if (Instrumentation != nullptr)
Instrumentation->failure(Name, Function, Failure, Duration);
Analyses.clear(Function);
@@ -869,8 +864,8 @@ PassManagerCallbacks<MachineFunction, MachineFunctionPass, MachinePassManagerOpt
Function.properties().applyTransition(Contract.Requires, Contract.Sets, Contract.Clears);
if (!Transition) {
Error Failure =
- prefixedError(Transition.error(), "pass '" + std::string(Name) +
- "' produced an invalid property transition: ");
+ withErrorContext(Transition.error(), "pass '" + std::string(Name) +
+ "' produced an invalid property transition: ");
if (Instrumentation != nullptr)
Instrumentation->failure(Name, Function, Failure, Duration);
Analyses.clear(Function);
@@ -884,8 +879,8 @@ PassManagerCallbacks<MachineFunction, MachineFunctionPass, MachinePassManagerOpt
VerifyOptions.Stage = Contract.VerificationStage;
auto Verification = MachineVerifier::verify(Function, Target, VerifyOptions);
if (!Verification) {
- Error Failure = prefixedError(Verification.error(),
- "pass '" + std::string(Name) + "' left invalid MachineIR: ");
+ Error Failure = withErrorContext(Verification.error(), "pass '" + std::string(Name) +
+ "' left invalid MachineIR: ");
if (Instrumentation != nullptr)
Instrumentation->failure(Name, Function, Failure, Duration);
Analyses.clear(Function);
@@ -972,7 +967,7 @@ PassManagerCallbacks<MachineFunction, MachineFunctionPass, MachinePassManagerOpt
Instrumentation->replayBufferedEvents(EventBuffers[Index]);
const auto &Failure = Failures[Index];
if (Failure) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
*Failure, "function '" + std::string(Unit.functionAt(Index)->name()) + "' failed: "));
}
}
@@ -1012,8 +1007,8 @@ PassManagerCallbacks<CompilationUnit, CompilationUnitPass, CompilationUnitPassMa
auto Verification = verifyCompilationUnit(Unit, Target);
if (!Verification) {
Error Failure =
- prefixedError(Verification.error(), "compilation-unit pass '" + std::string(Name) +
- "' received invalid MachineIR: ");
+ withErrorContext(Verification.error(), "compilation-unit pass '" + std::string(Name) +
+ "' received invalid MachineIR: ");
if (Instrumentation != nullptr)
Instrumentation->unitFailure(Name, Unit, Failure);
UnitAnalyses.setInstrumentation(nullptr);
@@ -1026,8 +1021,8 @@ PassManagerCallbacks<CompilationUnit, CompilationUnitPass, CompilationUnitPassMa
auto Result = UnitAnalyses.getResultByID(Required, Unit, Target);
if (!Result) {
Error Failure =
- prefixedError(Result.error(), "compilation-unit pass '" + std::string(Name) +
- "' could not materialize a required analysis: ");
+ withErrorContext(Result.error(), "compilation-unit pass '" + std::string(Name) +
+ "' could not materialize a required analysis: ");
if (Instrumentation != nullptr)
Instrumentation->unitFailure(Name, Unit, Failure);
UnitAnalyses.setInstrumentation(nullptr);
@@ -1042,8 +1037,8 @@ PassManagerCallbacks<CompilationUnit, CompilationUnitPass, CompilationUnitPassMa
auto Result = Pass->run(Unit, Target, UnitAnalyses, FunctionAnalyses);
const std::uint64_t Duration = elapsedNanoseconds(Start);
if (!Result) {
- Error Failure = prefixedError(Result.error(),
- "compilation-unit pass '" + std::string(Name) + "' failed: ");
+ Error Failure = withErrorContext(Result.error(), "compilation-unit pass '" +
+ std::string(Name) + "' failed: ");
if (Instrumentation != nullptr)
Instrumentation->unitFailure(Name, Unit, Failure, Duration);
UnitAnalyses.clearAll();
@@ -1077,8 +1072,8 @@ PassManagerCallbacks<CompilationUnit, CompilationUnitPass, CompilationUnitPassMa
auto Verification = verifyCompilationUnit(Unit, Target);
if (!Verification) {
Error Failure =
- prefixedError(Verification.error(), "compilation-unit pass '" + std::string(Name) +
- "' left invalid MachineIR: ");
+ withErrorContext(Verification.error(), "compilation-unit pass '" + std::string(Name) +
+ "' left invalid MachineIR: ");
if (Instrumentation != nullptr)
Instrumentation->unitFailure(Name, Unit, Failure, Duration);
UnitAnalyses.clearAll();
diff --git a/backend/lib/CodeGen/MachinePseudoExpansion.cpp b/backend/lib/CodeGen/MachinePseudoExpansion.cpp
index 663b148..e3109ed 100644
--- a/backend/lib/CodeGen/MachinePseudoExpansion.cpp
+++ b/backend/lib/CodeGen/MachinePseudoExpansion.cpp
@@ -195,11 +195,6 @@ using BlockSizeSnapshot = std::vector<std::pair<MachineBlockStableID, std::uint6
return TargetLate;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] std::vector<MachineInstr *> layoutInstructions(MachineFunction &Function) {
std::vector<MachineInstr *> Result;
for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next())
@@ -361,7 +356,7 @@ runPreparationTransaction(MachineFunction &Function, const TargetDescription &Ta
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "pseudo preparation setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "pseudo preparation setup failed: "));
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = prepareImplementation(*Trial->ClonedFunction, Target, Info, Actions);
if (!TrialResult)
@@ -369,8 +364,8 @@ runPreparationTransaction(MachineFunction &Function, const TargetDescription &Ta
Actions.beginReplay();
auto Result = prepareImplementation(Function, Target, Info, Actions);
if (!Result) {
- return std::unexpected(
- prefixedError(Result.error(), "pseudo preparation diverged after successful preflight: "));
+ return std::unexpected(withErrorContext(
+ Result.error(), "pseudo preparation diverged after successful preflight: "));
}
auto Replayed = Actions.finishReplay();
if (!Replayed)
@@ -387,7 +382,7 @@ runExpansionTransaction(MachineFunction &Function, const TargetDescription &Targ
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "pseudo expansion setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "pseudo expansion setup failed: "));
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult =
expandImplementation(*Trial->ClonedFunction, Target, Info, Phase, BranchInfo, Actions);
@@ -397,7 +392,7 @@ runExpansionTransaction(MachineFunction &Function, const TargetDescription &Targ
auto Result = expandImplementation(Function, Target, Info, Phase, BranchInfo, Actions);
if (!Result) {
return std::unexpected(
- prefixedError(Result.error(), "pseudo expansion diverged after successful preflight: "));
+ withErrorContext(Result.error(), "pseudo expansion diverged after successful preflight: "));
}
auto Replayed = Actions.finishReplay();
if (!Replayed)
diff --git a/backend/lib/CodeGen/MachineScheduler.cpp b/backend/lib/CodeGen/MachineScheduler.cpp
index 82f0c91..58c909d 100644
--- a/backend/lib/CodeGen/MachineScheduler.cpp
+++ b/backend/lib/CodeGen/MachineScheduler.cpp
@@ -49,11 +49,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] std::uint64_t saturatingAdd(std::uint64_t Left, std::uint64_t Right) noexcept {
return std::numeric_limits<std::uint64_t>::max() - Left < Right
? std::numeric_limits<std::uint64_t>::max()
@@ -406,7 +401,7 @@ Expected<MachinePassResult> MachineSchedulerPass::run(MachineFunction &Function,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "machine scheduler transaction setup failed: "));
+ withErrorContext(Trial.error(), "machine scheduler transaction setup failed: "));
}
MachineFunctionAnalysisManager TrialAnalyses;
auto TrialLiveness =
@@ -434,7 +429,7 @@ Expected<MachinePassResult> MachineSchedulerPass::run(MachineFunction &Function,
auto Run = runMachineScheduler(Function, Target, *Model, ScheduleDAGMode::PreRegisterAllocation,
**Liveness, **Frequencies, Options, &Actions);
if (!Run) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Run.error(), "machine scheduler diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/MachineSink.cpp b/backend/lib/CodeGen/MachineSink.cpp
index 176b75a..7c540e0 100644
--- a/backend/lib/CodeGen/MachineSink.cpp
+++ b/backend/lib/CodeGen/MachineSink.cpp
@@ -61,11 +61,6 @@ namespace {
return Stage == MachineCodeMotionStage::SelectedSSA ? Selected : PostRA;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -366,7 +361,8 @@ Expected<MachinePassResult> MachineSinkPass::run(MachineFunction &Function,
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
- return std::unexpected(prefixedError(Trial.error(), "machine sink transaction setup failed: "));
+ return std::unexpected(
+ withErrorContext(Trial.error(), "machine sink transaction setup failed: "));
}
MachineFunctionAnalysisManager TrialAnalyses;
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
@@ -375,7 +371,7 @@ Expected<MachinePassResult> MachineSinkPass::run(MachineFunction &Function,
auto Result = runImplementation(Function, Target, Analyses);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "machine sink diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineStackSlotColoring.cpp b/backend/lib/CodeGen/MachineStackSlotColoring.cpp
index 005872b..7a65eb3 100644
--- a/backend/lib/CodeGen/MachineStackSlotColoring.cpp
+++ b/backend/lib/CodeGen/MachineStackSlotColoring.cpp
@@ -41,11 +41,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
struct SlotInterval final {
MachineBasicBlock *Block = nullptr;
std::size_t First = 0;
@@ -242,14 +237,14 @@ Expected<MachinePassResult> MachineStackSlotColoringPass::run(MachineFunction &F
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "stack-slot coloring transaction setup failed: "));
+ withErrorContext(Trial.error(), "stack-slot coloring transaction setup failed: "));
}
auto TrialResult = runImplementation(*Trial->ClonedFunction);
if (!TrialResult)
return std::unexpected(TrialResult.error());
auto Result = runImplementation(Function);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "stack-slot coloring diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineTailDuplication.cpp b/backend/lib/CodeGen/MachineTailDuplication.cpp
index 8be1eb5..9214f40 100644
--- a/backend/lib/CodeGen/MachineTailDuplication.cpp
+++ b/backend/lib/CodeGen/MachineTailDuplication.cpp
@@ -49,11 +49,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -258,14 +253,14 @@ Expected<MachinePassResult> MachineTailDuplicationPass::run(MachineFunction &Fun
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "tail duplication transaction setup failed: "));
+ withErrorContext(Trial.error(), "tail duplication transaction setup failed: "));
}
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
if (!TrialResult)
return std::unexpected(TrialResult.error());
auto Result = runImplementation(Function, Target);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "tail duplication diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/MachineTailMerge.cpp b/backend/lib/CodeGen/MachineTailMerge.cpp
index dc07950..7467993 100644
--- a/backend/lib/CodeGen/MachineTailMerge.cpp
+++ b/backend/lib/CodeGen/MachineTailMerge.cpp
@@ -50,11 +50,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
[[nodiscard]] bool hasProtectedAttachment(const MachineInstr &Instruction) noexcept {
return Instruction.debugInstrNumber() != 0 || Instruction.preInstrSymbol().has_value() ||
Instruction.postInstrSymbol().has_value() || Instruction.heapAllocMarker().has_value() ||
@@ -307,14 +302,15 @@ Expected<MachinePassResult> MachineTailMergePass::run(MachineFunction &Function,
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
- return std::unexpected(prefixedError(Trial.error(), "tail merge transaction setup failed: "));
+ return std::unexpected(
+ withErrorContext(Trial.error(), "tail merge transaction setup failed: "));
}
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
if (!TrialResult)
return std::unexpected(TrialResult.error());
auto Result = runImplementation(Function, Target);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "tail merge diverged after a successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/PHIElimination.cpp b/backend/lib/CodeGen/PHIElimination.cpp
index 6de449f..f57a950 100644
--- a/backend/lib/CodeGen/PHIElimination.cpp
+++ b/backend/lib/CodeGen/PHIElimination.cpp
@@ -64,11 +64,6 @@ struct PhiPlan final {
std::vector<PhiIncoming> IncomingValues;
};
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
[[nodiscard]] Expected<void> requireProperties(const MachineFunction &Function,
const MachinePassContract &Contract) {
const MachineFunctionProperties Missing = Contract.Requires.missingFrom(Function.properties());
@@ -172,7 +167,7 @@ buildCopy(MachineFunction &Function, const TargetDescription &Target,
auto Legality = TargetInfo.validateCopy(Destination.Reg, Destination.SubReg, Source.Reg,
Source.SubReg, Function, Target);
if (!Legality)
- return std::unexpected(prefixedError(Legality.error(), "illegal COPY: "));
+ return std::unexpected(withErrorContext(Legality.error(), "illegal COPY: "));
MachineIRBuilder Builder(Function, Target);
auto Point =
@@ -500,7 +495,7 @@ Expected<MachinePassResult> PHIEliminationPass::run(MachineFunction &Function,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "PHI-elimination transaction setup failed: "));
+ withErrorContext(Trial.error(), "PHI-elimination transaction setup failed: "));
}
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
@@ -510,7 +505,7 @@ Expected<MachinePassResult> PHIEliminationPass::run(MachineFunction &Function,
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "PHI elimination diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
@@ -601,7 +596,7 @@ Expected<MachinePassResult> PHIEliminationPass::runImplementation(MachineFunctio
auto Repaired = repairTrackedLiveness(Function, Target, Result);
if (!Repaired)
return std::unexpected(
- prefixedError(Repaired.error(), "PHI-elimination liveness repair failed: "));
+ withErrorContext(Repaired.error(), "PHI-elimination liveness repair failed: "));
}
Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
return Result;
diff --git a/backend/lib/CodeGen/PostRAScheduler.cpp b/backend/lib/CodeGen/PostRAScheduler.cpp
index b5f7ae9..fede8df 100644
--- a/backend/lib/CodeGen/PostRAScheduler.cpp
+++ b/backend/lib/CodeGen/PostRAScheduler.cpp
@@ -49,11 +49,6 @@ namespace {
return Contract;
}
-[[nodiscard]] Error prefixedError(const Error &Failure, std::string Prefix) {
- Prefix.append(Failure.message());
- return Error(Failure.code(), std::move(Prefix));
-}
-
} // namespace
const MachinePassContract &PostRASchedulerPass::contract() const noexcept {
@@ -72,7 +67,7 @@ Expected<MachinePassResult> PostRASchedulerPass::run(MachineFunction &Function,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
return std::unexpected(
- prefixedError(Trial.error(), "post-RA scheduler transaction setup failed: "));
+ withErrorContext(Trial.error(), "post-RA scheduler transaction setup failed: "));
}
MachineFunctionAnalysisManager TrialAnalyses;
auto TrialLiveness =
@@ -104,7 +99,7 @@ Expected<MachinePassResult> PostRASchedulerPass::run(MachineFunction &Function,
auto Run = runMachineScheduler(Function, Target, *Model, ScheduleDAGMode::PostRegisterAllocation,
**Liveness, **Frequencies, Options, &Actions);
if (!Run) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Run.error(), "post-RA scheduler diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp b/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp
index c935d6a..d51a93b 100644
--- a/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp
+++ b/backend/lib/CodeGen/PrologueEpilogueInsertion.cpp
@@ -3,11 +3,11 @@
#include "rcc/CodeGen/PrologueEpilogueInsertion.h"
#include "rcc/CodeGen/FrameIndexElimination.h"
+#include "rcc/CodeGen/LiveVariables.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
-#include "rcc/CodeGen/LiveVariables.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineVerifier.h"
@@ -19,14 +19,7 @@
#include <vector>
namespace rcc {
-namespace {
-
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
-} // namespace
+namespace {} // namespace
const MachinePassContract &PrologueEpilogueInsertionPass::contract() const noexcept {
static const MachinePassContract Contract = [] {
@@ -63,15 +56,16 @@ Expected<MachinePassResult> PrologueEpilogueInsertionPass::run(MachineFunction &
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial)
- return std::unexpected(prefixedError(Trial.error(), "frame transaction setup failed: "));
+ return std::unexpected(withErrorContext(Trial.error(), "frame transaction setup failed: "));
auto TrialRun = runImplementation(*Trial->ClonedFunction, Target);
if (!TrialRun)
- return std::unexpected(prefixedError(TrialRun.error(), "frame transaction rejected input: "));
+ return std::unexpected(
+ withErrorContext(TrialRun.error(), "frame transaction rejected input: "));
auto Transition = Trial->ClonedFunction->properties().applyTransition(
contract().Requires, contract().Sets, contract().Clears);
if (!Transition) {
return std::unexpected(
- prefixedError(Transition.error(), "frame transaction property update failed: "));
+ withErrorContext(Transition.error(), "frame transaction property update failed: "));
}
auto Verified =
MachineVerifier::verify(*Trial->ClonedFunction, Target,
@@ -80,12 +74,12 @@ Expected<MachinePassResult> PrologueEpilogueInsertionPass::run(MachineFunction &
.VerifyProperties = true});
if (!Verified) {
return std::unexpected(
- prefixedError(Verified.error(), "frame transaction postcondition failed: "));
+ withErrorContext(Verified.error(), "frame transaction postcondition failed: "));
}
auto Result = runImplementation(Function, Target);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "frame lowering diverged after successful transaction preflight: "));
}
return Result;
diff --git a/backend/lib/CodeGen/RegAllocBasic.cpp b/backend/lib/CodeGen/RegAllocBasic.cpp
index 7f730ca..c3ed98b 100644
--- a/backend/lib/CodeGen/RegAllocBasic.cpp
+++ b/backend/lib/CodeGen/RegAllocBasic.cpp
@@ -128,7 +128,7 @@ Expected<MachinePassResult> BasicAllocatorPass::run(MachineFunction &Function,
BasicAllocatorEngine Allocator(Function, Target, *Info, Analyses, Options);
auto Allocated = Allocator.run();
if (!Allocated) {
- return std::unexpected(regalloc_detail::withPrefix(
+ return std::unexpected(withErrorContext(
Allocated.error(), "BasicAllocator diverged after a successful transaction preflight: "));
}
auto Reserved = regalloc_detail::reserveFunctionRegisters(Function, FunctionReserved);
diff --git a/backend/lib/CodeGen/RegAllocFast.cpp b/backend/lib/CodeGen/RegAllocFast.cpp
index 73c3e1a..7fb1471 100644
--- a/backend/lib/CodeGen/RegAllocFast.cpp
+++ b/backend/lib/CodeGen/RegAllocFast.cpp
@@ -605,7 +605,7 @@ Expected<MachinePassResult> FastAllocatorPass::run(MachineFunction &Function,
FastAllocatorEngine Allocator(Function, Target, *Info, **Liveness);
auto Allocated = Allocator.run();
if (!Allocated) {
- return std::unexpected(regalloc_detail::withPrefix(
+ return std::unexpected(withErrorContext(
Allocated.error(), "FastAllocator diverged after a successful transaction preflight: "));
}
auto Reserved = regalloc_detail::reserveFunctionRegisters(Function, FunctionReserved);
diff --git a/backend/lib/CodeGen/RegAllocInternal.h b/backend/lib/CodeGen/RegAllocInternal.h
index 0becb92..52840cf 100644
--- a/backend/lib/CodeGen/RegAllocInternal.h
+++ b/backend/lib/CodeGen/RegAllocInternal.h
@@ -26,9 +26,6 @@
#include <optional>
#include <ranges>
#include <span>
-#include <string>
-#include <string_view>
-#include <utility>
#include <vector>
namespace rcc::regalloc_detail {
@@ -39,12 +36,6 @@ struct AllocationRunResult final {
bool ChangedFrame = false;
};
-[[nodiscard]] inline Error withPrefix(const Error &Failure, std::string_view Prefix) {
- std::string Message(Prefix);
- Message.append(Failure.message());
- return Error(Failure.code(), std::move(Message));
-}
-
[[nodiscard]] inline Expected<Register> virtualRegisterAt(std::size_t Index) {
auto Reg = Register::makeVirtual(Index);
if (!Reg)
diff --git a/backend/lib/CodeGen/RegAllocPBQP.cpp b/backend/lib/CodeGen/RegAllocPBQP.cpp
index 6706cd5..758f756 100644
--- a/backend/lib/CodeGen/RegAllocPBQP.cpp
+++ b/backend/lib/CodeGen/RegAllocPBQP.cpp
@@ -327,7 +327,7 @@ Expected<MachinePassResult> PBQPAllocatorPass::run(MachineFunction &Function,
PBQPAllocatorEngine Allocator(Function, Target, *Info, Analyses, Options, &Actions);
auto Allocated = Allocator.run();
if (!Allocated) {
- return std::unexpected(regalloc_detail::withPrefix(
+ return std::unexpected(withErrorContext(
Allocated.error(), "PBQPAllocator diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
diff --git a/backend/lib/CodeGen/TwoAddressInstructionPass.cpp b/backend/lib/CodeGen/TwoAddressInstructionPass.cpp
index b6f2a25..b9e1443 100644
--- a/backend/lib/CodeGen/TwoAddressInstructionPass.cpp
+++ b/backend/lib/CodeGen/TwoAddressInstructionPass.cpp
@@ -32,11 +32,6 @@ struct RegisterValue final {
std::uint32_t TargetFlags = 0;
};
-[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
- Prefix.append(Source.message());
- return Error(Source.code(), std::move(Prefix));
-}
-
[[nodiscard]] Expected<void> requireProperties(const MachineFunction &Function,
const MachinePassContract &Contract) {
const MachineFunctionProperties Missing = Contract.Requires.missingFrom(Function.properties());
@@ -67,7 +62,7 @@ buildCopy(MachineFunction &Function, const TargetDescription &Target,
auto Legality = TargetInfo.validateCopy(Destination.Reg, Destination.SubReg, Source.Reg,
Source.SubReg, Function, Target);
if (!Legality)
- return std::unexpected(prefixedError(Legality.error(), "illegal COPY: "));
+ return std::unexpected(withErrorContext(Legality.error(), "illegal COPY: "));
MachineIRBuilder Builder(Function, Target);
auto Point =
@@ -470,7 +465,8 @@ TwoAddressInstructionPass::run(MachineFunction &Function, const TargetDescriptio
Function, Target,
MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
if (!Trial) {
- return std::unexpected(prefixedError(Trial.error(), "two-address transaction setup failed: "));
+ return std::unexpected(
+ withErrorContext(Trial.error(), "two-address transaction setup failed: "));
}
MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
@@ -480,7 +476,7 @@ TwoAddressInstructionPass::run(MachineFunction &Function, const TargetDescriptio
Actions.beginReplay();
auto Result = runImplementation(Function, Target, Actions);
if (!Result) {
- return std::unexpected(prefixedError(
+ return std::unexpected(withErrorContext(
Result.error(), "two-address rewrite diverged after a successful transaction preflight: "));
}
auto Replayed = Actions.finishReplay();
@@ -652,7 +648,7 @@ Expected<MachinePassResult> TwoAddressInstructionPass::runImplementation(
auto Repaired = repairTrackedLiveness(Function, Target, Result);
if (!Repaired)
return std::unexpected(
- prefixedError(Repaired.error(), "two-address liveness repair failed: "));
+ withErrorContext(Repaired.error(), "two-address liveness repair failed: "));
}
Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
return Result;
diff --git a/backend/lib/Support/Error.cpp b/backend/lib/Support/Error.cpp
index 2bda044..d4793b5 100644
--- a/backend/lib/Support/Error.cpp
+++ b/backend/lib/Support/Error.cpp
@@ -61,4 +61,9 @@ std::string_view getErrorCodeName(ErrorCode Code) noexcept {
Error::Error(ErrorCode Code, std::string Message) : Code(Code), Message(std::move(Message)) {}
+Error withErrorContext(const Error &Failure, std::string Context) {
+ Context.append(Failure.message());
+ return Error(Failure.code(), std::move(Context));
+}
+
} // namespace rcc