Skip to content

Commit 5887006

Browse files
authored
[IR] Forbid mixing condition and operand bundle assumes (#160460)
Assumes either have a boolean condition, or a number of attribute based operand bundles. Currently, we also allow mixing both forms, though we don't make use of this in practice. This adds additional complexity for code dealing with assumes. Forbid mixing both forms, by requiring that assumes with operand bundles have an i1 true condition.
1 parent 88a2f40 commit 5887006

File tree

7 files changed

+28
-18
lines changed

7 files changed

+28
-18
lines changed

llvm/docs/LangRef.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,8 @@ assumptions, such as that a :ref:`parameter attribute <paramattrs>` or a
30133013
location. Operand bundles enable assumptions that are either hard or impossible
30143014
to represent as a boolean argument of an :ref:`llvm.assume <int_assume>`.
30153015

3016+
Assumes with operand bundles must have ``i1 true`` as the condition operand.
3017+
30163018
An assume operand bundle has the form:
30173019

30183020
::
@@ -3045,7 +3047,7 @@ allows the optimizer to assume that at location of call to
30453047

30463048
.. code-block:: llvm
30473049

3048-
call void @llvm.assume(i1 %cond) ["cold"(), "nonnull"(ptr %val)]
3050+
call void @llvm.assume(i1 true) ["cold"(), "nonnull"(ptr %val)]
30493051

30503052
allows the optimizer to assume that the :ref:`llvm.assume <int_assume>`
30513053
call location is cold and that ``%val`` may not be null.

llvm/lib/IR/Verifier.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5675,6 +5675,11 @@ void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
56755675
default:
56765676
break;
56775677
case Intrinsic::assume: {
5678+
if (Call.hasOperandBundles()) {
5679+
auto *Cond = dyn_cast<ConstantInt>(Call.getArgOperand(0));
5680+
Check(Cond && Cond->isOne(),
5681+
"assume with operand bundles must have i1 true condition", Call);
5682+
}
56785683
for (auto &Elem : Call.bundle_op_infos()) {
56795684
unsigned ArgCount = Elem.End - Elem.Begin;
56805685
// Separate storage assumptions are special insofar as they're the only

llvm/test/Transforms/AlignmentFromAssumptions/domtree-crash.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
define void @fn1() {
1111
; CHECK-LABEL: define void @fn1() {
12-
; CHECK-NEXT: call void @llvm.assume(i1 false) [ "align"(ptr @global, i64 1) ]
12+
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "align"(ptr @global, i64 1) ]
1313
; CHECK-NEXT: ret void
1414
;
15-
call void @llvm.assume(i1 false) [ "align"(ptr @global, i64 1) ]
15+
call void @llvm.assume(i1 true) [ "align"(ptr @global, i64 1) ]
1616
ret void
1717
}
1818

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,24 +498,24 @@ not_taken:
498498
define i1 @nonnull3B(ptr %a, i1 %control) {
499499
; CHECK-LABEL: @nonnull3B(
500500
; CHECK-NEXT: entry:
501+
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[A:%.*]], align 8
501502
; CHECK-NEXT: br i1 [[CONTROL:%.*]], label [[TAKEN:%.*]], label [[NOT_TAKEN:%.*]]
502503
; CHECK: taken:
503-
; CHECK-NEXT: [[LOAD:%.*]] = load ptr, ptr [[A:%.*]], align 8
504-
; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[LOAD]], null
505-
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]]) [ "nonnull"(ptr [[LOAD]]) ]
506-
; CHECK-NEXT: ret i1 [[CMP]]
504+
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[LOAD]]) ]
505+
; CHECK-NEXT: ret i1 true
507506
; CHECK: not_taken:
507+
; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[LOAD]]) ]
508508
; CHECK-NEXT: ret i1 false
509509
;
510510
entry:
511511
%load = load ptr, ptr %a
512512
%cmp = icmp ne ptr %load, null
513513
br i1 %control, label %taken, label %not_taken
514514
taken:
515-
call void @llvm.assume(i1 %cmp) ["nonnull"(ptr %load)]
515+
call void @llvm.assume(i1 true) ["nonnull"(ptr %load)]
516516
ret i1 %cmp
517517
not_taken:
518-
call void @llvm.assume(i1 %cmp) ["nonnull"(ptr %load)]
518+
call void @llvm.assume(i1 true) ["nonnull"(ptr %load)]
519519
ret i1 %control
520520
}
521521

@@ -544,7 +544,7 @@ taken:
544544
br label %exit
545545
exit:
546546
; FIXME: this shouldn't be dropped because it is still dominated by the new position of %load
547-
call void @llvm.assume(i1 %cmp) ["nonnull"(ptr %load)]
547+
call void @llvm.assume(i1 %cmp)
548548
ret i1 %cmp2
549549
not_taken:
550550
call void @llvm.assume(i1 %cmp)
@@ -575,7 +575,7 @@ taken:
575575
exit:
576576
ret i1 %cmp2
577577
not_taken:
578-
call void @llvm.assume(i1 %cmp) ["nonnull"(ptr %load)]
578+
call void @llvm.assume(i1 %cmp)
579579
ret i1 %control
580580
}
581581

llvm/test/Verifier/assume-bundles.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
declare void @llvm.assume(i1)
55

6-
define void @func(ptr %P, i32 %P1, ptr %P2, ptr %P3) {
6+
define void @func(ptr %P, i32 %P1, ptr %P2, ptr %P3, i1 %cond) {
77
; CHECK: tags must be valid attribute names
88
; CHECK: "adazdazd"
99
call void @llvm.assume(i1 true) ["adazdazd"()]
@@ -32,5 +32,7 @@ define void @func(ptr %P, i32 %P1, ptr %P2, ptr %P3) {
3232
call void @llvm.assume(i1 true) ["separate_storage"(ptr %P, i32 123)]
3333
; CHECK: dereferenceable assumptions should have 2 arguments
3434
call void @llvm.assume(i1 true) ["align"(ptr %P, i32 4), "dereferenceable"(ptr %P)]
35+
; CHECK: assume with operand bundles must have i1 true condition
36+
call void @llvm.assume(i1 %cond) ["nonnull"(ptr %P)]
3537
ret void
3638
}

mlir/test/Target/LLVMIR/Import/intrinsic.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,12 +733,12 @@ define void @assume(i1 %true) {
733733
}
734734

735735
; CHECK-LABEL: @assume_with_opbundles
736-
; CHECK-SAME: %[[TRUE:[a-zA-Z0-9]+]]
737736
; CHECK-SAME: %[[PTR:[a-zA-Z0-9]+]]
738-
define void @assume_with_opbundles(i1 %true, ptr %p) {
737+
define void @assume_with_opbundles(ptr %p) {
738+
; CHECK: %[[TRUE:.+]] = llvm.mlir.constant(true) : i1
739739
; CHECK: %[[ALIGN:.+]] = llvm.mlir.constant(8 : i32) : i32
740740
; CHECK: llvm.intr.assume %[[TRUE]] ["align"(%[[PTR]], %[[ALIGN]] : !llvm.ptr, i32)] : i1
741-
call void @llvm.assume(i1 %true) ["align"(ptr %p, i32 8)]
741+
call void @llvm.assume(i1 true) ["align"(ptr %p, i32 8)]
742742
ret void
743743
}
744744

mlir/test/Target/LLVMIR/llvmir-intrinsics.mlir

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,11 @@ llvm.func @assume_without_opbundles(%cond: i1) {
460460
}
461461

462462
// CHECK-LABEL: @assume_with_opbundles
463-
llvm.func @assume_with_opbundles(%cond: i1, %p: !llvm.ptr) {
463+
llvm.func @assume_with_opbundles(%p: !llvm.ptr) {
464+
%true = llvm.mlir.constant(true) : i1
464465
%0 = llvm.mlir.constant(8 : i32) : i32
465-
// CHECK: call void @llvm.assume(i1 %{{.+}}) [ "align"(ptr %{{.+}}, i32 8) ]
466-
llvm.intr.assume %cond ["align"(%p, %0 : !llvm.ptr, i32)] : i1
466+
// CHECK: call void @llvm.assume(i1 true) [ "align"(ptr %{{.+}}, i32 8) ]
467+
llvm.intr.assume %true ["align"(%p, %0 : !llvm.ptr, i32)] : i1
467468
llvm.return
468469
}
469470

0 commit comments

Comments
 (0)