-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[clang] Support constexpr alignment of __builtin_assume_aligned. #127223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-codegen Author: Tang Jiajun (tangjj11) ChangesFull diff: https://github.com/llvm/llvm-project/pull/127223.diff 2 Files Affected:
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()); }
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right fix.
After we check the constant expression in Sema, we should create a ConstantExpr containing the the evaluated value, so we don't need to re-evaluate it later.
thanks, I will close the PR. |
No description provided.