Skip to content

[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

Closed
wants to merge 5 commits into from

Conversation

tangjj11
Copy link

No description provided.

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Feb 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 14, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Tang Jiajun (tangjj11)

Changes

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 --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()); }

Copy link
Collaborator

@efriedma-quic efriedma-quic left a 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.

@tangjj11
Copy link
Author

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.

@tangjj11 tangjj11 closed this Feb 15, 2025
@tangjj11 tangjj11 deleted the builtin_assume_aligned_bug branch February 15, 2025 03:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants