Skip to content

Commit

Permalink
[MLIR][LLVM] Allowlist more side-effecting operations in LLVM inliner.
Browse files Browse the repository at this point in the history
These operations do not on their own require special handling to be
inlined, and can just be inlined as is.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D147148
  • Loading branch information
definelicht committed Mar 30, 2023
1 parent 243b355 commit a33018d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
27 changes: 19 additions & 8 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMInlining.cpp
Expand Up @@ -289,14 +289,25 @@ struct LLVMInlinerInterface : public DialectInlinerInterface {
return false;
}
}
if (!isa<LLVM::CallOp, LLVM::AllocaOp, LLVM::LifetimeStartOp,
LLVM::LifetimeEndOp, LLVM::LoadOp, LLVM::StoreOp>(op)) {
LLVM_DEBUG(llvm::dbgs()
<< "Cannot inline: unhandled side effecting operation \""
<< op->getName() << "\"\n");
return false;
}
return true;
// clang-format off
if (isa<LLVM::AllocaOp,
LLVM::CallOp,
LLVM::DbgDeclareOp,
LLVM::DbgValueOp,
LLVM::LifetimeEndOp,
LLVM::LifetimeStartOp,
LLVM::LoadOp,
LLVM::MemcpyOp,
LLVM::MemmoveOp,
LLVM::MemsetOp,
LLVM::StoreOp,
LLVM::UnreachableOp>(op))
return true;
// clang-format on
LLVM_DEBUG(llvm::dbgs()
<< "Cannot inline: unhandled side effecting operation \""
<< op->getName() << "\"\n");
return false;
}

/// Handle the given inlined return by replacing it with a branch. This
Expand Down
28 changes: 24 additions & 4 deletions mlir/test/Dialect/LLVMIR/inlining.mlir
@@ -1,18 +1,38 @@
// RUN: mlir-opt %s -inline -split-input-file | FileCheck %s

#file = #llvm.di_file<"foo.mlir" in "/foo/">
#variable = #llvm.di_local_variable<scope = #file>
#variableAddr = #llvm.di_local_variable<scope = #file>

func.func @inner_func_inlinable(%ptr : !llvm.ptr) -> i32 {
%0 = llvm.mlir.constant(42 : i32) : i32
llvm.store %0, %ptr { alignment = 8 } : i32, !llvm.ptr
%1 = llvm.load %ptr { alignment = 8 } : !llvm.ptr -> i32
llvm.intr.dbg.value #variable = %0 : i32
llvm.intr.dbg.declare #variableAddr = %ptr : !llvm.ptr
%byte = llvm.mlir.constant(43 : i8) : i8
%volatile = llvm.mlir.constant(1 : i1) : i1
"llvm.intr.memset"(%ptr, %byte, %0, %volatile) : (!llvm.ptr, i8, i32, i1) -> ()
"llvm.intr.memmove"(%ptr, %ptr, %0, %volatile) : (!llvm.ptr, !llvm.ptr, i32, i1) -> ()
"llvm.intr.memcpy"(%ptr, %ptr, %0, %volatile) : (!llvm.ptr, !llvm.ptr, i32, i1) -> ()
llvm.cond_br %volatile, ^bb1, ^bb2
^bb1:
llvm.unreachable
^bb2:
return %1 : i32
}

// CHECK-LABEL: func.func @test_inline(
// CHECK-SAME: %[[PTR:[a-zA-Z0-9_]+]]
// CHECK-NEXT: %[[CST:.*]] = llvm.mlir.constant(42 : i32) : i32
// CHECK-NEXT: llvm.store %[[CST]], %[[PTR]]
// CHECK-NEXT: %[[RES:.+]] = llvm.load %[[PTR]]
// CHECK-NEXT: return %[[RES]] : i32
// CHECK: %[[CST:.*]] = llvm.mlir.constant(42
// CHECK: llvm.store %[[CST]], %[[PTR]]
// CHECK: %[[RES:.+]] = llvm.load %[[PTR]]
// CHECK: llvm.intr.dbg.value #{{.+}} = %[[CST]]
// CHECK: llvm.intr.dbg.declare #{{.+}} = %[[PTR]]
// CHECK: "llvm.intr.memset"(%[[PTR]]
// CHECK: "llvm.intr.memmove"(%[[PTR]], %[[PTR]]
// CHECK: "llvm.intr.memcpy"(%[[PTR]], %[[PTR]]
// CHECK: llvm.unreachable
func.func @test_inline(%ptr : !llvm.ptr) -> i32 {
%0 = call @inner_func_inlinable(%ptr) : (!llvm.ptr) -> i32
return %0 : i32
Expand Down

0 comments on commit a33018d

Please sign in to comment.