Author: Augie Fackler Date: 2026-11-16T17:31:47-05:00 New Revision: b225dc1cce12953689d013aad1d58035ba751976
URL: https://github.com/llvm/llvm-project/commit/b225dc1cce12953689d013aad1d58035ba751976 DIFF: https://github.com/llvm/llvm-project/commit/b225dc1cce12953689d013aad1d58035ba751976.diff LOG: [NFC] clang: simplify emitAlignmentAssumptionCheck (#239728) Move the alignment check condition (`ptrtoint`, optional offset subtraction, mask, and `icmp eq 0`) into `CodeGenFunction::emitAlignmentAssumptionCheck` and emit the sanitizer check before `Builder.CreateAlignmentAssumption`. This removes the need to pass `TheCheck` and `Assumption` into `emitAlignmentAssumptionCheck` (or temporarily unlink and re-insert `Assumption`) and prepares `emitAlignmentAssumptionCheck` to be called directly without an accompanying `@llvm.assume`. This patch is new since this line of work was originally authored in 2023. It was largely written by an LLM in response to review feedback for the next patch, but I hand-reviewed the content and it looks reasonable enough (with the same disclaimer as other patches that I know nothing about clang.) Added: Modified: clang/lib/CodeGen/CodeGenFunction.cpp clang/lib/CodeGen/CodeGenFunction.h Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 33434526d8632..bcb00eb3c75ca 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -2790,33 +2790,11 @@ void CodeGenFunction::emitAlignmentAssumption(llvm::Value *PtrValue, if (OffsetValue && OffsetValue->getType() != IntPtrTy) OffsetValue = Builder.CreateIntCast(OffsetValue, IntPtrTy, true, "casted.offset"); - llvm::Value *TheCheck = nullptr; - if (SanOpts.has(SanitizerKind::Alignment)) { - llvm::Value *PtrIntValue = - Builder.CreatePtrToInt(PtrValue, IntPtrTy, "ptrint"); - - if (OffsetValue) { - bool IsOffsetZero = false; - if (const auto *CI = dyn_cast<llvm::ConstantInt>(OffsetValue)) - IsOffsetZero = CI->isZero(); - - if (!IsOffsetZero) - PtrIntValue = Builder.CreateSub(PtrIntValue, OffsetValue, "offsetptr"); - } - - llvm::Value *Zero = llvm::ConstantInt::get(IntPtrTy, 0); - llvm::Value *Mask = - Builder.CreateSub(Alignment, llvm::ConstantInt::get(IntPtrTy, 1)); - llvm::Value *MaskedPtr = Builder.CreateAnd(PtrIntValue, Mask, "maskedptr"); - TheCheck = Builder.CreateICmpEQ(MaskedPtr, Zero, "maskcond"); - } - llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption( - CGM.getDataLayout(), PtrValue, Alignment, OffsetValue); - - if (!SanOpts.has(SanitizerKind::Alignment)) - return; - emitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment, - OffsetValue, TheCheck, Assumption); + if (SanOpts.has(SanitizerKind::Alignment)) + emitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment, + OffsetValue); + Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment, + OffsetValue); } void CodeGenFunction::emitAlignmentAssumption(llvm::Value *PtrValue, @@ -3313,56 +3291,61 @@ void CodeGenFunction::EmitX86MultiVersionResolver( // Loc - where the diagnostic will point, where in the source code this // alignment has failed. -// SecondaryLoc - if present (will be present if sufficiently diff erent from +// AssumptionLoc - if present (will be present if sufficiently diff erent from // Loc), the diagnostic will additionally point a "Note:" to this location. // It should be the location where the __attribute__((assume_aligned)) // was written e.g. -void CodeGenFunction::emitAlignmentAssumptionCheck( - llvm::Value *Ptr, QualType Ty, SourceLocation Loc, - SourceLocation SecondaryLoc, llvm::Value *Alignment, - llvm::Value *OffsetValue, llvm::Value *TheCheck, - llvm::Instruction *Assumption) { - assert(isa_and_nonnull<llvm::CallInst>(Assumption) && - cast<llvm::CallInst>(Assumption)->getCalledOperand() == - llvm::Intrinsic::getOrInsertDeclaration(Builder.getModule(), - llvm::Intrinsic::assume) && - "Assumption should be a call to llvm.assume()."); - assert(&(Builder.GetInsertBlock()->back()) == Assumption && - "Assumption should be the last instruction of the basic block, " - "since the basic block is still being generated."); - +void CodeGenFunction::emitAlignmentAssumptionCheck(llvm::Value *Ptr, + QualType Ty, + SourceLocation Loc, + SourceLocation AssumptionLoc, + llvm::Value *Alignment, + llvm::Value *OffsetValue) { if (!SanOpts.has(SanitizerKind::Alignment)) return; + if (Alignment->getType() != IntPtrTy) + Alignment = + Builder.CreateIntCast(Alignment, IntPtrTy, false, "casted.align"); + if (OffsetValue && OffsetValue->getType() != IntPtrTy) + OffsetValue = + Builder.CreateIntCast(OffsetValue, IntPtrTy, true, "casted.offset"); + + llvm::Value *PtrIntValue = Builder.CreatePtrToInt(Ptr, IntPtrTy, "ptrint"); + + if (OffsetValue) { + bool IsOffsetZero = false; + if (const auto *CI = dyn_cast<llvm::ConstantInt>(OffsetValue)) + IsOffsetZero = CI->isZero(); + + if (!IsOffsetZero) + PtrIntValue = Builder.CreateSub(PtrIntValue, OffsetValue, "offsetptr"); + } + + llvm::Value *Zero = llvm::ConstantInt::get(IntPtrTy, 0); + llvm::Value *Mask = + Builder.CreateSub(Alignment, llvm::ConstantInt::get(IntPtrTy, 1)); + llvm::Value *MaskedPtr = Builder.CreateAnd(PtrIntValue, Mask, "maskedptr"); + llvm::Value *TheCheck = Builder.CreateICmpEQ(MaskedPtr, Zero, "maskcond"); + // Don't check pointers to volatile data. The behavior here is implementation- // defined. if (Ty->getPointeeType().isVolatileQualified()) return; - // We need to temorairly remove the assumption so we can insert the - // sanitizer check before it, else the check will be dropped by optimizations. - Assumption->removeFromParent(); - - { - auto CheckOrdinal = SanitizerKind::SO_Alignment; - auto CheckHandler = SanitizerHandler::AlignmentAssumption; - SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler); + auto CheckOrdinal = SanitizerKind::SO_Alignment; + auto CheckHandler = SanitizerHandler::AlignmentAssumption; + SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler); - if (!OffsetValue) - OffsetValue = Builder.getInt1(false); // no offset. - - llvm::Constant *StaticData[] = {EmitCheckSourceLocation(Loc), - EmitCheckSourceLocation(SecondaryLoc), - EmitCheckTypeDescriptor(Ty)}; - llvm::Value *DynamicData[] = {Ptr, Alignment, OffsetValue}; - EmitCheck({std::make_pair(TheCheck, CheckOrdinal)}, CheckHandler, - StaticData, DynamicData); - } + if (!OffsetValue) + OffsetValue = Builder.getInt1(false); // no offset. - // We are now in the (new, empty) "cont" basic block. - // Reintroduce the assumption. - Builder.Insert(Assumption); - // FIXME: Assumption still has it's original basic block as it's Parent. + llvm::Constant *StaticData[] = {EmitCheckSourceLocation(Loc), + EmitCheckSourceLocation(AssumptionLoc), + EmitCheckTypeDescriptor(Ty)}; + llvm::Value *DynamicData[] = {Ptr, Alignment, OffsetValue}; + EmitCheck({std::make_pair(TheCheck, CheckOrdinal)}, CheckHandler, StaticData, + DynamicData); } llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) { diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index f3c3ef3b94da0..6afd94a4b16c7 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -3707,9 +3707,7 @@ class CodeGenFunction : public CodeGenTypeCache { SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, - llvm::Value *OffsetValue, - llvm::Value *TheCheck, - llvm::Instruction *Assumption); + llvm::Value *OffsetValue = nullptr); void emitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
