Mehdi Amini 973ddb7d6e Define a NoTerminator traits that allows operations with a single block region to not provide a terminator
In particular for Graph Regions, the terminator needs is just a
historical artifact of the generalization of MLIR from CFG region.
Operations like Module don't need a terminator, and before Module
migrated to be an operation with region there wasn't any needed.

To validate the feature, the ModuleOp is migrated to use this trait and
the ModuleTerminator operation is deleted.

This patch is likely to break clients, if you're in this case:

- you may iterate on a ModuleOp with `getBody()->without_terminator()`,
  the solution is simple: just remove the ->without_terminator!
- you created a builder with `Builder::atBlockTerminator(module_body)`,
  just use `Builder::atBlockEnd(module_body)` instead.
- you were handling ModuleTerminator: it isn't needed anymore.
- for generic code, a `Block::mayNotHaveTerminator()` may be used.

Differential Revision: https://reviews.llvm.org/D98468
2021-03-25 03:59:03 +00:00

58 lines
1.9 KiB
Python

# RUN: %PYTHON %s | FileCheck %s
from mlir.ir import *
from mlir.dialects import builtin
from mlir.dialects import linalg
from mlir.dialects import std
def run(f):
print("\nTEST:", f.__name__)
f()
# CHECK-LABEL: TEST: testStructuredOpOnTensors
def testStructuredOpOnTensors():
with Context() as ctx, Location.unknown():
module = Module.create()
f32 = F32Type.get()
tensor_type = RankedTensorType.get((2, 3, 4), f32)
with InsertionPoint(module.body):
func = builtin.FuncOp(name="matmul_test",
type=FunctionType.get(
inputs=[tensor_type, tensor_type],
results=[tensor_type]))
with InsertionPoint(func.add_entry_block()):
lhs, rhs = func.entry_block.arguments
result = linalg.MatmulOp([lhs, rhs], results=[tensor_type]).result
std.ReturnOp([result])
# CHECK: %[[R:.*]] = linalg.matmul ins(%arg0, %arg1 : tensor<2x3x4xf32>, tensor<2x3x4xf32>) -> tensor<2x3x4xf32>
print(module)
run(testStructuredOpOnTensors)
# CHECK-LABEL: TEST: testStructuredOpOnBuffers
def testStructuredOpOnBuffers():
with Context() as ctx, Location.unknown():
module = Module.create()
f32 = F32Type.get()
memref_type = MemRefType.get((2, 3, 4), f32)
with InsertionPoint(module.body):
func = builtin.FuncOp(name="matmul_test",
type=FunctionType.get(
inputs=[memref_type, memref_type, memref_type],
results=[]))
with InsertionPoint(func.add_entry_block()):
lhs, rhs, result = func.entry_block.arguments
linalg.MatmulOp([lhs, rhs], outputs=[result])
std.ReturnOp([])
# CHECK: linalg.matmul ins(%arg0, %arg1 : memref<2x3x4xf32>, memref<2x3x4xf32>) outs(%arg2 : memref<2x3x4xf32>)
print(module)
run(testStructuredOpOnBuffers)