Skip to content

Commit 758be97

Browse files
definelichtgysit
authored andcommitted
[mlir:LLVM] Rudimentary inlining support for LLVM load store.
Conservatively only allow inlining for loads and stores that don't carry any attributes that require handling while inlining. This can later be relaxed when proper handling is introduced. Reviewed By: Dinistro, gysit Differential Revision: https://reviews.llvm.org/D141115
1 parent 51e36f2 commit 758be97

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,10 +2830,25 @@ namespace {
28302830
struct LLVMInlinerInterface : public DialectInlinerInterface {
28312831
using DialectInlinerInterface::DialectInlinerInterface;
28322832

2833-
/// Conservatively only allow inlining of pure ops.
2833+
/// Conservative allowlist-based inlining of operations supported so far.
28342834
bool isLegalToInline(Operation *op, Region *, bool,
28352835
BlockAndValueMapping &) const final {
2836-
return isPure(op);
2836+
if (isPure(op))
2837+
return true;
2838+
return llvm::TypeSwitch<Operation *, bool>(op)
2839+
.Case<LLVM::LoadOp, LLVM::StoreOp>([&](auto memOp) {
2840+
// Some attributes on load and store operations require handling
2841+
// during inlining. Since this is not yet implemented, refuse to
2842+
// inline memory operations that have any of these attributes.
2843+
if (memOp.getAccessGroups())
2844+
return false;
2845+
if (memOp.getAliasScopes())
2846+
return false;
2847+
if (memOp.getNoaliasScopes())
2848+
return false;
2849+
return true;
2850+
})
2851+
.Default([](auto) { return false; });
28372852
}
28382853
};
28392854
} // end anonymous namespace
Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1-
// RUN: mlir-opt %s -inline | FileCheck %s
1+
// RUN: mlir-opt %s -inline -split-input-file | FileCheck %s
22

3-
// CHECK-LABEL: func.func @test_inline() -> i32 {
4-
// CHECK-NEXT: %[[RES:.*]] = llvm.mlir.constant(42 : i32) : i32
3+
func.func @inner_func_inlinable(%ptr : !llvm.ptr) -> i32 {
4+
%0 = llvm.mlir.constant(42 : i32) : i32
5+
llvm.store %0, %ptr { alignment = 8 } : i32, !llvm.ptr
6+
%1 = llvm.load %ptr { alignment = 8 } : !llvm.ptr -> i32
7+
return %1 : i32
8+
}
9+
10+
// CHECK-LABEL: func.func @test_inline(
11+
// CHECK-SAME: %[[PTR:[a-zA-Z0-9_]+]]
12+
// CHECK-NEXT: %[[CST:.*]] = llvm.mlir.constant(42 : i32) : i32
13+
// CHECK-NEXT: llvm.store %[[CST]], %[[PTR]]
14+
// CHECK-NEXT: %[[RES:.+]] = llvm.load %[[PTR]]
515
// CHECK-NEXT: return %[[RES]] : i32
6-
func.func @test_inline() -> i32 {
7-
%0 = call @inner_func_inlinable() : () -> i32
16+
func.func @test_inline(%ptr : !llvm.ptr) -> i32 {
17+
%0 = call @inner_func_inlinable(%ptr) : (!llvm.ptr) -> i32
818
return %0 : i32
919
}
1020

11-
func.func @inner_func_inlinable() -> i32 {
12-
%0 = llvm.mlir.constant(42 : i32) : i32
13-
return %0 : i32
21+
// -----
22+
23+
func.func @inner_func_not_inlinable() -> !llvm.ptr<f64> {
24+
%0 = llvm.mlir.constant(0 : i32) : i32
25+
%1 = llvm.alloca %0 x f64 : (i32) -> !llvm.ptr<f64>
26+
return %1 : !llvm.ptr<f64>
1427
}
1528

1629
// CHECK-LABEL: func.func @test_not_inline() -> !llvm.ptr<f64> {
@@ -21,8 +34,25 @@ func.func @test_not_inline() -> !llvm.ptr<f64> {
2134
return %0 : !llvm.ptr<f64>
2235
}
2336

24-
func.func @inner_func_not_inlinable() -> !llvm.ptr<f64> {
25-
%0 = llvm.mlir.constant(0 : i32) : i32
26-
%1 = llvm.alloca %0 x f64 : (i32) -> !llvm.ptr<f64>
27-
return %1 : !llvm.ptr<f64>
37+
// -----
38+
39+
llvm.metadata @metadata {
40+
llvm.access_group @group
41+
llvm.return
42+
}
43+
44+
func.func private @with_mem_attr(%ptr : !llvm.ptr) -> () {
45+
%0 = llvm.mlir.constant(42 : i32) : i32
46+
// Do not inline load/store operations that carry attributes requiring
47+
// handling while inlining, until this is supported by the inliner.
48+
llvm.store %0, %ptr { access_groups = [@metadata::@group] }: i32, !llvm.ptr
49+
return
50+
}
51+
52+
// CHECK-LABEL: func.func @test_not_inline
53+
// CHECK-NEXT: call @with_mem_attr
54+
// CHECK-NEXT: return
55+
func.func @test_not_inline(%ptr : !llvm.ptr) -> () {
56+
call @with_mem_attr(%ptr) : (!llvm.ptr) -> ()
57+
return
2858
}

0 commit comments

Comments
 (0)