[llvm-branch-commits] [clang] [NFC] clang: simplify emitAlignmentAssumptionCheck (PR #239728)
Augie Fackler via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Nov 16 12:49:32 PST 2026
https://github.com/durin42 updated https://github.com/llvm/llvm-project/pull/239728
>From 30e2ff7dd7b0834b285bdec58634bde4d4505de0 Mon Sep 17 00:00:00 2001
From: Augie Fackler <augie at google.com>
Date: Wed, 4 Nov 2026 13:04:34 -0500
Subject: [PATCH] [NFC] clang: simplify emitAlignmentAssumptionCheck
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.)
---
clang/lib/CodeGen/CodeGenFunction.cpp | 111 +++++++++++---------------
clang/lib/CodeGen/CodeGenFunction.h | 4 +-
2 files changed, 48 insertions(+), 67 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 33434526d86325..bcb00eb3c75ca8 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 different from
+// AssumptionLoc - if present (will be present if sufficiently different 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 f3c3ef3b94da0a..6afd94a4b16c74 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,
More information about the llvm-branch-commits
mailing list