https://github.com/tangjj11 created https://github.com/llvm/llvm-project/pull/127223
None >From 5c83ee47a5927645ded9ec0af061ff21c094f008 Mon Sep 17 00:00:00 2001 From: Tang Jiajun <1220586...@qq.com> Date: Sat, 15 Feb 2025 00:35:34 +0800 Subject: [PATCH 1/2] Add test. --- clang/test/CodeGen/builtin-assume-aligned.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 clang/test/CodeGen/builtin-assume-aligned.cpp 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()); } >From ada1d827164472676fc3cc36fb8b0bca5e053286 Mon Sep 17 00:00:00 2001 From: Tang Jiajun <1220586...@qq.com> Date: Sat, 15 Feb 2025 00:37:25 +0800 Subject: [PATCH 2/2] support constexpr alignment of __builtin_assume_aligned. --- clang/lib/CodeGen/CGBuiltin.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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: { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits