Skip to content

Conversation

tbaederr
Copy link
Contributor

Since the second argument must be a constant integer, we can as well convert it to a ConstantExpr in Sema.

Fixes #161272

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 30, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 30, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Since the second argument must be a constant integer, we can as well convert it to a ConstantExpr in Sema.

Fixes #161272


Full diff: https://github.com/llvm/llvm-project/pull/161314.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaChecking.cpp (+3)
  • (modified) clang/test/SemaCXX/builtin-assume-aligned.cpp (+6)
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 39c3aa2243338..7b37e0b8d5430 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -5954,6 +5954,9 @@ bool Sema::BuiltinAssumeAligned(CallExpr *TheCall) {
     if (Result > Sema::MaximumAlignment)
       Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
           << SecondArg->getSourceRange() << Sema::MaximumAlignment;
+
+    TheCall->setArg(1,
+                    ConstantExpr::Create(Context, SecondArg, APValue(Result)));
   }
 
   if (NumArgs > 2) {
diff --git a/clang/test/SemaCXX/builtin-assume-aligned.cpp b/clang/test/SemaCXX/builtin-assume-aligned.cpp
index 48bd8414fc50a..afc11cc694705 100644
--- a/clang/test/SemaCXX/builtin-assume-aligned.cpp
+++ b/clang/test/SemaCXX/builtin-assume-aligned.cpp
@@ -47,3 +47,9 @@ constexpr void *s1 = __builtin_assume_aligned(x, 32);
 constexpr void *s2 = __builtin_assume_aligned(x, 32, 5);
 constexpr void *s3 = __builtin_assume_aligned(x, 32, -1);
 
+
+constexpr int add(int a, int b) {
+  return a+b;
+}
+constexpr void *c1 = __builtin_assume_aligned(p, add(1,1));
+constexpr void *c2 = __builtin_assume_aligned(p, add(2,1)); // expected-error {{not a power of 2}}

@cor3ntin
Copy link
Contributor

Can you add a changelog entry? Thanks

@tbaederr tbaederr merged commit 8f77621 into llvm:main Oct 1, 2025
8 of 10 checks passed
@alanzhao1
Copy link
Contributor

Heads up - this PR is causing us to hit the following assertion in https://github.com/google/highway. I'm currently running cvise to get a minimal reproducer.

bin/clang -o /dev/null -O3 ~/Downloads/out.ii -std=gnu++20
clang-21: /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGValue.h:274: void clang::CodeGen::LValue::initializeSimpleLValue(Address, QualType, LValueBaseInfo, TBAAAccessInfo, ASTContext &): Assertion `Addr.getBasePointer()->getType()->isPointerTy()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin/clang-21 -cc1 -triple x86_64-unknown-linux-gnu -O3 -emit-obj -dumpdir /dev/null- -disable-free -clear-ast-before-backend -main-file-name out.ii -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/usr/local/google/home/ayzhao/src/llvm-project/build-debug -fcoverage-compilation-dir=/usr/local/google/home/ayzhao/src/llvm-project/build-debug -resource-dir /usr/local/google/home/ayzhao/src/llvm-project/build-debug/lib/clang/22 -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fmessage-length=160 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -vectorize-loops -vectorize-slp -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/out-66e06c.o -x c++-cpp-output /usr/local/google/home/ayzhao/Downloads/out.ii
1.      <eof> parser at end of file
2.      third_party/highway/hwy/aligned_allocator.cc:27:11: LLVM IR generation of declaration 'hwy'
3.      third_party/highway/hwy/aligned_allocator.cc:69:21: Generating code for declaration 'hwy::AllocateAlignedBytes'
 #0 0x000056406ce4bbfd llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:834:11
 #1 0x000056406ce4c12b PrintStackTraceSignalHandler(void*) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:917:1
 #2 0x000056406ce4a03f llvm::sys::RunSignalHandlers() /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Signals.cpp:104:5
 #3 0x000056406ce4c7c9 SignalHandler(int, siginfo_t*, void*) /usr/local/google/home/ayzhao/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:426:38
 #4 0x00007f81e2649df0 (/lib/x86_64-linux-gnu/libc.so.6+0x3fdf0)
 #5 0x00007f81e269e95c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x00007f81e2649cc2 raise ./signal/../sysdeps/posix/raise.c:27:6
 #7 0x00007f81e26324ac abort ./stdlib/abort.c:81:3
 #8 0x00007f81e2632420 __assert_perror_fail ./assert/assert-perr.c:31:1
 #9 0x000056406d3b969a clang::CodeGen::LValue::initializeSimpleLValue(clang::CodeGen::Address, clang::QualType, clang::CodeGen::LValueBaseInfo, clang::CodeGen::TBAAAccessInfo, clang::ASTContext&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGValue.h:275:3
#10 0x000056406d3ab630 clang::CodeGen::LValue::MakeAddr(clang::CodeGen::Address, clang::QualType, clang::ASTContext&, clang::CodeGen::LValueBaseInfo, clang::CodeGen::TBAAAccessInfo) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGValue.h:436:7
#11 0x000056406d47784e clang::CodeGen::CodeGenFunction::EmitLoadOfScalar(clang::CodeGen::Address, bool, clang::QualType, clang::SourceLocation, clang::CodeGen::LValueBaseInfo, clang::CodeGen::TBAAAccessInfo, bool) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExpr.cpp:2129:7
#12 0x000056406d3aad30 clang::CodeGen::CodeGenFunction::EmitLoadOfScalar(clang::CodeGen::Address, bool, clang::QualType, clang::SourceLocation, clang::CodeGen::AlignmentSource, bool) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenFunction.h:4310:5
#13 0x000056406d6bee94 (anonymous namespace)::ScalarExprEmitter::VisitConstantExpr(clang::ConstantExpr*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:469:9
#14 0x000056406d6b6979 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) /usr/local/google/home/ayzhao/src/llvm-project/build-debug/tools/clang/include/clang/AST/StmtNodes.inc:404:1
#15 0x000056406d6ac425 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:452:52
#16 0x000056406d6ac262 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:5882:3
#17 0x000056406dafbc39 clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGBuiltin.cpp:3542:12
#18 0x000056406d48648c clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExpr.cpp:5968:12
#19 0x000056406d6c0061 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:631:36
#20 0x000056406d6b6be1 clang::StmtVisitorBase<std::add_pointer, (anonymous namespace)::ScalarExprEmitter, llvm::Value*>::Visit(clang::Stmt*) /usr/local/google/home/ayzhao/src/llvm-project/build-debug/tools/clang/include/clang/AST/StmtNodes.inc:608:1
#21 0x000056406d6ac425 (anonymous namespace)::ScalarExprEmitter::Visit(clang::Expr*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:452:52
#22 0x000056406d6ac262 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGExprScalar.cpp:5882:3
#23 0x000056406d4eb0b5 clang::CodeGen::CodeGenFunction::EmitReturnStmt(clang::ReturnStmt const&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:1698:26
#24 0x000056406d4e744f clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef<clang::Attr const*>) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:163:75
#25 0x000056406d4f1733 clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CGStmt.cpp:591:3
#26 0x000056406d4bca3f clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:1376:5
#27 0x000056406d4bdbe6 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:1629:3
#28 0x000056406d2847df clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:6401:3
#29 0x000056406d27b1ac clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:4484:12
#30 0x000056406d280545 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:4195:5
#31 0x000056406d2798e8 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:7309:5
#32 0x000056406d28b971 clang::CodeGen::CodeGenModule::EmitDeclContext(clang::DeclContext const*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:7280:16
#33 0x000056406d279a55 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenModule.cpp:7338:5
#34 0x000056406dc73462 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/ModuleBuilder.cpp:195:20
#35 0x000056406dc62831 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:171:8
#36 0x00005640708caa93 clang::ParseAST(clang::Sema&, bool, bool) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Parse/ParseAST.cpp:174:11
#37 0x000056406e42fb29 clang::ASTFrontendAction::ExecuteAction() /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1436:1
#38 0x000056406dc671ed clang::CodeGenAction::ExecuteAction() /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/CodeGen/CodeGenAction.cpp:1110:5
#39 0x000056406e42f506 clang::FrontendAction::Execute() /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Frontend/FrontendAction.cpp:1318:7
#40 0x000056406e33e173 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/Frontend/CompilerInstance.cpp:1007:23
#41 0x000056406e6125c5 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /usr/local/google/home/ayzhao/src/llvm-project/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:310:8
#42 0x000056406a512c8a cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/cc1_main.cpp:300:13
#43 0x000056406a5061c8 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/driver.cpp:225:5
#44 0x000056406a504fb1 clang_main(int, char**, llvm::ToolContext const&) /usr/local/google/home/ayzhao/src/llvm-project/clang/tools/driver/driver.cpp:268:5
#45 0x000056406a538fd5 main /usr/local/google/home/ayzhao/src/llvm-project/build-debug/tools/clang/tools/driver/clang-driver.cpp:17:3
#46 0x00007f81e2633ca8 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#47 0x00007f81e2633d65 call_init ./csu/../csu/libc-start.c:128:20
#48 0x00007f81e2633d65 __libc_start_main ./csu/../csu/libc-start.c:347:5
#49 0x000056406a504a21 _start (/usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin/clang-21+0x64afa21)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 22.0.0git (git@github.com:llvm/llvm-project.git 71365c0b6b95eb6874b08f32fe58b5419979ff4c)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/ayzhao/src/llvm-project/build-debug/bin
Build config: +unoptimized, +assertions
clang: note: diagnostic msg: Error generating preprocessed source(s) - no preprocessable inputs.

@alanzhao1
Copy link
Contributor

Heads up - this PR is causing us to hit the following assertion in https://github.com/google/highway. I'm currently running cvise to get a minimal reproducer.

The repro file is 2.5M, so cvise will take forever. This will take a while, so I apologize in advance.

@alanzhao1
Copy link
Contributor

Reduced repro:

constexpr long kAlignment = 128;
long AllocateAlignedBytes_payload;
void AllocateAlignedBytes() {
  __builtin_assume_aligned(
      reinterpret_cast<void *>(AllocateAlignedBytes_payload), kAlignment);
}

@cor3ntin
Copy link
Contributor

cor3ntin commented Oct 2, 2025

@tbaederr

DKLoehr added a commit to DKLoehr/llvm-project that referenced this pull request Oct 2, 2025
@DKLoehr
Copy link
Contributor

DKLoehr commented Oct 2, 2025

Since this is a crash and it's blocking us from finding any other issues at chromium, let's revert this for now.

@erichkeane
Copy link
Collaborator

Since this is a crash and it's blocking us from finding any other issues at chromium, let's revert this for now.

That sounds unfortunate but reasonable. Thanks for handling the revert.

@erichkeane
Copy link
Collaborator

@Fznamznon : Your patch 9ad72df seems at least closely related here: in CGExprScalar.cpp in VisitConstantExpr.

This patch is unfortunately just revealing a bug there, where the type of the constant expression is a L value constant but isn't getting the 'pointer' type for the EmitLoadOfScalar.

It isn't clear to me what SHOULD happen, but that at least seems wrong.

DKLoehr added a commit that referenced this pull request Oct 2, 2025
…stantExpr (#161314)" (#161719)

This reverts commit 8f77621 (#161314).

Reason: Causing crashes when building https://github.com/google/highway.
See the original PR for details.
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
llvm#161314)

Since the second argument must be a constant integer, we can as well
convert it to a `ConstantExpr` in Sema.


Fixes llvm#161272
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…stantExpr (llvm#161314)" (llvm#161719)

This reverts commit 8f77621 (llvm#161314).

Reason: Causing crashes when building https://github.com/google/highway.
See the original PR for details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] __builtin_assume_aligned() with constexpr argument crashes clang
6 participants