From 8558f80b4077536d6aa46b97328d6df1210e261f Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Thu, 13 Aug 2026 13:18:05 +0800 Subject: [Support] Centralize error context handling --- .../lib/CodeGen/GlobalISel/InstructionSelect.cpp | 31 +++++++++------------- backend/lib/CodeGen/GlobalISel/Legalizer.cpp | 17 +++++------- backend/lib/CodeGen/GlobalISel/RegBankSelect.cpp | 27 +++++++++---------- 3 files changed, 31 insertions(+), 44 deletions(-) (limited to 'backend/lib/CodeGen/GlobalISel') 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 repairTrackedLiveness(MachineFunction &Function, const TargetDescription &Target, MachinePassResult &Result) { @@ -207,7 +202,7 @@ Expected 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 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 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 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 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 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 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 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 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 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 repairTrackedLiveness(MachineFunction &Function, const TargetDescription &Target, MachinePassResult &Result) { @@ -173,7 +168,7 @@ Expected 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 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 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; -- cgit v1.2.3