[clang] [clang] Support constexpr alignment of __builtin_assume_aligned. (PR #127223)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 14 08:44:56 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-codegen
Author: Tang Jiajun (tangjj11)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/127223.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGBuiltin.cpp (+12-8)
- (added) clang/test/CodeGen/builtin-assume-aligned.cpp (+17)
``````````diff
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index d57f491a20c8e..bff5451a01300 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3829,14 +3829,18 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
(E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) : nullptr;
Value *AlignmentValue = EmitScalarExpr(E->getArg(1));
- ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue);
- if (AlignmentCI->getValue().ugt(llvm::Value::MaximumAlignment))
- AlignmentCI = ConstantInt::get(AlignmentCI->getIntegerType(),
- llvm::Value::MaximumAlignment);
-
- emitAlignmentAssumption(PtrValue, Ptr,
- /*The expr loc is sufficient.*/ SourceLocation(),
- AlignmentCI, OffsetValue);
+ if (ConstantInt *AlignmentCI = cast<ConstantInt>(AlignmentValue)) {
+ if (AlignmentCI->getValue().ugt(llvm::Value::MaximumAlignment))
+ AlignmentCI = ConstantInt::get(AlignmentCI->getIntegerType(),
+ llvm::Value::MaximumAlignment);
+
+ emitAlignmentAssumption(PtrValue, Ptr,
+ /*The expr loc is sufficient.*/ SourceLocation(),
+ AlignmentCI, OffsetValue);
+ } else
+ emitAlignmentAssumption(PtrValue, Ptr,
+ /*The expr loc is sufficient.*/ SourceLocation(),
+ AlignmentValue, OffsetValue);
return RValue::get(PtrValue);
}
case Builtin::BI__builtin_assume_dereferenceable: {
diff --git a/clang/test/CodeGen/builtin-assume-aligned.cpp b/clang/test/CodeGen/builtin-assume-aligned.cpp
new file mode 100644
index 0000000000000..fae4221d7d7e7
--- /dev/null
+++ b/clang/test/CodeGen/builtin-assume-aligned.cpp
@@ -0,0 +1,17 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
+
+constexpr int get_align() { return 1; }
+
+// CHECK-LABEL: @Ztest1P(
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[A_ADDR:%.*]] = alloca ptr, align 8
+// CHECK-NEXT: store ptr [[A:%.*]], ptr [[A_ADDR]], align 8
+// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8
+// CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr [[TMP0]], i64 32, i64 0) ]
+// CHECK-NEXT: store ptr [[TMP0]], ptr [[A_ADDR]], align 8
+// CHECK-NEXT: [[TMP3:%.*]] = load ptr, ptr [[A_ADDR]], align 8
+// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[TMP3]], i64 0
+// CHECK-NEXT: [[TMP4:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
+// CHECK-NEXT: ret i32 [[TMP4]]
+void *test1(void *a) { return __builtin_assume_aligned(a, 32, get_align()); }
``````````
</details>
https://github.com/llvm/llvm-project/pull/127223
More information about the cfe-commits
mailing list