Skip to content

Commit 20f56e1

Browse files
authored
[CodeGen] Add default lowering for llvm.allow.{runtime,ubsan}.check() (#86049)
RFC: https://discourse.llvm.org/t/rfc-add-llvm-experimental-hot-intrinsic-or-llvm-hot/77641
1 parent a21e0ba commit 20f56e1

File tree

10 files changed

+165
-1
lines changed

10 files changed

+165
-1
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2462,8 +2462,10 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
24622462
break;
24632463
case Intrinsic::assume:
24642464
llvm_unreachable("llvm.assume should have been removed already");
2465+
case Intrinsic::allow_runtime_check:
2466+
case Intrinsic::allow_ubsan_check:
24652467
case Intrinsic::experimental_widenable_condition: {
2466-
// Give up on future widening oppurtunties so that we can fold away dead
2468+
// Give up on future widening opportunities so that we can fold away dead
24672469
// paths and merge blocks before going into block-local instruction
24682470
// selection.
24692471
if (II->use_empty()) {

llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,11 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
24952495
return translateTrap(CI, MIRBuilder, TargetOpcode::G_DEBUGTRAP);
24962496
case Intrinsic::ubsantrap:
24972497
return translateTrap(CI, MIRBuilder, TargetOpcode::G_UBSANTRAP);
2498+
case Intrinsic::allow_runtime_check:
2499+
case Intrinsic::allow_ubsan_check:
2500+
MIRBuilder.buildCopy(getOrCreateVReg(CI),
2501+
getOrCreateVReg(*ConstantInt::getTrue(CI.getType())));
2502+
return true;
24982503
case Intrinsic::amdgcn_cs_chain:
24992504
return translateCallBase(CI, MIRBuilder);
25002505
case Intrinsic::fptrunc_round: {

llvm/lib/CodeGen/IntrinsicLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
243243
break;
244244
}
245245

246+
case Intrinsic::allow_runtime_check:
247+
case Intrinsic::allow_ubsan_check:
248+
CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getType()));
249+
return;
250+
246251
case Intrinsic::ctpop:
247252
CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getArgOperand(0), CI));
248253
break;

llvm/lib/CodeGen/SelectionDAG/FastISel.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,10 @@ bool FastISel::selectIntrinsicCall(const IntrinsicInst *II) {
14611461
case Intrinsic::is_constant:
14621462
llvm_unreachable("llvm.is.constant.* should have been lowered already");
14631463

1464+
case Intrinsic::allow_runtime_check:
1465+
case Intrinsic::allow_ubsan_check:
1466+
llvm_unreachable("llvm.*.check should have been lowered already");
1467+
14641468
case Intrinsic::launder_invariant_group:
14651469
case Intrinsic::strip_invariant_group:
14661470
case Intrinsic::expect: {

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7326,6 +7326,11 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
73267326
return;
73277327
}
73287328

7329+
case Intrinsic::allow_runtime_check:
7330+
case Intrinsic::allow_ubsan_check:
7331+
setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
7332+
return;
7333+
73297334
case Intrinsic::uadd_with_overflow:
73307335
case Intrinsic::sadd_with_overflow:
73317336
case Intrinsic::usub_with_overflow:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc < %s -mtriple=aarch64 -global-isel=0 -fast-isel=0 | FileCheck %s
3+
; RUN: llc < %s -mtriple=aarch64 -global-isel=1 -fast-isel=0 | FileCheck %s
4+
; RUN: llc < %s -mtriple=aarch64 -global-isel=0 -fast-isel=1 | FileCheck %s
5+
6+
target triple = "aarch64-linux"
7+
8+
define i1 @test_runtime() local_unnamed_addr {
9+
; CHECK-LABEL: test_runtime:
10+
; CHECK: // %bb.0: // %entry
11+
; CHECK-NEXT: mov w0, #1 // =0x1
12+
; CHECK-NEXT: ret
13+
entry:
14+
%allow = call i1 @llvm.allow.runtime.check(metadata !"test_check")
15+
ret i1 %allow
16+
}
17+
18+
declare i1 @llvm.allow.runtime.check(metadata) nounwind
19+
20+
define i1 @test_ubsan() local_unnamed_addr {
21+
; CHECK-LABEL: test_ubsan:
22+
; CHECK: // %bb.0: // %entry
23+
; CHECK-NEXT: mov w0, #1 // =0x1
24+
; CHECK-NEXT: ret
25+
entry:
26+
%allow = call i1 @llvm.allow.ubsan.check(i8 7)
27+
ret i1 %allow
28+
}
29+
30+
declare i1 @llvm.allow.ubsan.check(i8) nounwind
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc < %s -mtriple=amdgcn-amd-mesa3d -global-isel=0 -fast-isel=0 | FileCheck %s
3+
; RUN: llc < %s -mtriple=amdgcn-amd-mesa3d -global-isel=1 -fast-isel=0 | FileCheck %s
4+
; RUN: llc < %s -mtriple=amdgcn-amd-mesa3d -global-isel=0 -fast-isel=1 | FileCheck %s
5+
6+
define i1 @test_runtime() local_unnamed_addr {
7+
; CHECK-LABEL: test_runtime:
8+
; CHECK: ; %bb.0: ; %entry
9+
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
10+
; CHECK-NEXT: v_mov_b32_e32 v0, 1
11+
; CHECK-NEXT: s_setpc_b64 s[30:31]
12+
entry:
13+
%allow = call i1 @llvm.allow.runtime.check(metadata !"test_check")
14+
ret i1 %allow
15+
}
16+
17+
declare i1 @llvm.allow.runtime.check(metadata) nounwind
18+
19+
define i1 @test_ubsan() local_unnamed_addr {
20+
; CHECK-LABEL: test_ubsan:
21+
; CHECK: ; %bb.0: ; %entry
22+
; CHECK-NEXT: s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
23+
; CHECK-NEXT: v_mov_b32_e32 v0, 1
24+
; CHECK-NEXT: s_setpc_b64 s[30:31]
25+
entry:
26+
%allow = call i1 @llvm.allow.ubsan.check(i8 7)
27+
ret i1 %allow
28+
}
29+
30+
declare i1 @llvm.allow.ubsan.check(i8) nounwind
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; RUN: llc < %s -O3 -global-isel=0 -fast-isel=0
2+
; RUN: llc < %s -O3 -global-isel=1 -fast-isel=0
3+
; RUN: llc < %s -O3 -global-isel=0 -fast-isel=1
4+
5+
; RUN: llc < %s -O0 -global-isel=0 -fast-isel=0
6+
; RUN: llc < %s -O0 -global-isel=1 -fast-isel=0
7+
; RUN: llc < %s -O0 -global-isel=0 -fast-isel=1
8+
9+
define i1 @test_runtime() local_unnamed_addr {
10+
entry:
11+
%allow = call i1 @llvm.allow.runtime.check(metadata !"test_check")
12+
ret i1 %allow
13+
}
14+
15+
declare i1 @llvm.allow.runtime.check(metadata) nounwind
16+
17+
define i1 @test_ubsan() local_unnamed_addr {
18+
entry:
19+
%allow = call i1 @llvm.allow.ubsan.check(i8 7)
20+
ret i1 %allow
21+
}
22+
23+
declare i1 @llvm.allow.ubsan.check(i8) nounwind
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=riscv32 -global-isel=0 -fast-isel=0 | FileCheck %s
3+
; RUN: llc < %s -mtriple=riscv32 -global-isel=1 -fast-isel=0 | FileCheck %s
4+
; RUN: llc < %s -mtriple=riscv32 -global-isel=0 -fast-isel=1 | FileCheck %s
5+
6+
; RUN: llc < %s -mtriple=riscv64 -global-isel=0 -fast-isel=0 | FileCheck %s
7+
; RUN: llc < %s -mtriple=riscv64 -global-isel=1 -fast-isel=0 | FileCheck %s
8+
; RUN: llc < %s -mtriple=riscv64 -global-isel=0 -fast-isel=1 | FileCheck %s
9+
10+
define i1 @test_runtime() local_unnamed_addr {
11+
; CHECK-LABEL: test_runtime:
12+
; CHECK: # %bb.0: # %entry
13+
; CHECK-NEXT: li a0, 1
14+
; CHECK-NEXT: ret
15+
entry:
16+
%allow = call i1 @llvm.allow.runtime.check(metadata !"test_check")
17+
ret i1 %allow
18+
}
19+
20+
declare i1 @llvm.allow.runtime.check(metadata) nounwind
21+
22+
define i1 @test_ubsan() local_unnamed_addr {
23+
; CHECK-LABEL: test_ubsan:
24+
; CHECK: # %bb.0: # %entry
25+
; CHECK-NEXT: li a0, 1
26+
; CHECK-NEXT: ret
27+
entry:
28+
%allow = call i1 @llvm.allow.ubsan.check(i8 7)
29+
ret i1 %allow
30+
}
31+
32+
declare i1 @llvm.allow.ubsan.check(i8) nounwind

llvm/test/CodeGen/X86/allow-check.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
2+
; RUN: llc < %s -mtriple=x86_64 -global-isel=0 -fast-isel=0 | FileCheck %s
3+
; RUN: llc < %s -mtriple=x86_64 -global-isel=1 -fast-isel=0 | FileCheck %s
4+
; RUN: llc < %s -mtriple=x86_64 -global-isel=0 -fast-isel=1 | FileCheck %s
5+
6+
define i1 @test_runtime() local_unnamed_addr {
7+
; CHECK-LABEL: test_runtime:
8+
; CHECK: # %bb.0: # %entry
9+
; CHECK-NEXT: movb $1, %al
10+
; CHECK-NEXT: retq
11+
entry:
12+
%allow = call i1 @llvm.allow.runtime.check(metadata !"test_check")
13+
ret i1 %allow
14+
}
15+
16+
declare i1 @llvm.allow.runtime.check(metadata) nounwind
17+
18+
define i1 @test_ubsan() local_unnamed_addr {
19+
; CHECK-LABEL: test_ubsan:
20+
; CHECK: # %bb.0: # %entry
21+
; CHECK-NEXT: movb $1, %al
22+
; CHECK-NEXT: retq
23+
entry:
24+
%allow = call i1 @llvm.allow.ubsan.check(i8 7)
25+
ret i1 %allow
26+
}
27+
28+
declare i1 @llvm.allow.ubsan.check(i8) nounwind

0 commit comments

Comments
 (0)