mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-27 02:16:06 +00:00

This implements the C++23 `[[assume]]` attribute. Assumption information is lowered to a call to `@llvm.assume`, unless the expression has side-effects, in which case it is discarded and a warning is issued to tell the user that the assumption doesn’t do anything. A failed assumption at compile time is an error (unless we are in `MSVCCompat` mode, in which case we don’t check assumptions at compile time). Due to performance regressions in LLVM, assumptions can be disabled with the `-fno-assumptions` flag. With it, assumptions will still be parsed and checked, but no calls to `@llvm.assume` will be emitted and assumptions will not be checked at compile time.
51 lines
1.5 KiB
C++
51 lines
1.5 KiB
C++
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++23 %s -emit-llvm -o - | FileCheck %s
|
|
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++23 -fno-assumptions %s -emit-llvm -o - | FileCheck %s --check-prefix=DISABLED
|
|
|
|
// DISABLED-NOT: @llvm.assume
|
|
|
|
bool f();
|
|
|
|
template <typename T>
|
|
void f2() {
|
|
[[assume(sizeof(T) == sizeof(int))]];
|
|
}
|
|
|
|
// CHECK: @_Z1gii(i32 noundef [[X:%.*]], i32 noundef [[Y:%.*]])
|
|
// CHECK-NEXT: entry:
|
|
// CHECK-NEXT: [[X_ADDR:%.*]] = alloca i32
|
|
// CHECK-NEXT: [[Y_ADDR:%.*]] = alloca i32
|
|
// CHECK-NEXT: store i32 [[X]], ptr [[X_ADDR]]
|
|
// CHECK-NEXT: store i32 [[Y]], ptr [[Y_ADDR]]
|
|
void g(int x, int y) {
|
|
// Not emitted because it has side-effects.
|
|
[[assume(f())]];
|
|
|
|
// CHECK-NEXT: call void @llvm.assume(i1 true)
|
|
[[assume((1, 2))]];
|
|
|
|
// CHECK-NEXT: [[X1:%.*]] = load i32, ptr [[X_ADDR]]
|
|
// CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[X1]], 27
|
|
// CHECK-NEXT: call void @llvm.assume(i1 [[CMP1]])
|
|
[[assume(x != 27)]];
|
|
|
|
// CHECK-NEXT: [[X2:%.*]] = load i32, ptr [[X_ADDR]]
|
|
// CHECK-NEXT: [[Y2:%.*]] = load i32, ptr [[Y_ADDR]]
|
|
// CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[X2]], [[Y2]]
|
|
// CHECK-NEXT: call void @llvm.assume(i1 [[CMP2]])
|
|
[[assume(x == y)]];
|
|
|
|
// CHECK-NEXT: call void @_Z2f2IiEvv()
|
|
f2<int>();
|
|
|
|
// CHECK-NEXT: call void @_Z2f2IdEvv()
|
|
f2<double>();
|
|
}
|
|
|
|
// CHECK: void @_Z2f2IiEvv()
|
|
// CHECK-NEXT: entry:
|
|
// CHECK-NEXT: call void @llvm.assume(i1 true)
|
|
|
|
// CHECK: void @_Z2f2IdEvv()
|
|
// CHECK-NEXT: entry:
|
|
// CHECK-NEXT: call void @llvm.assume(i1 false)
|