Skip to content

Commit 7d54e5d

Browse files
committed
Merge remote-tracking branch 'origin/feature/fused-ops' into bump_to_2d50029f
2 parents 6f28929 + 19bdfae commit 7d54e5d

File tree

85 files changed

+3141
-247
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3141
-247
lines changed

mlir/include/mlir-c/Pass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ mlirPassManagerRunOnOp(MlirPassManager passManager, MlirOperation op);
7878
MLIR_CAPI_EXPORTED void
7979
mlirPassManagerEnableIRPrinting(MlirPassManager passManager);
8080

81+
/// Enable lir-reproducer-before-all.
82+
MLIR_CAPI_EXPORTED void
83+
mlirPassManagerEnableReproducerBeforeAll(MlirPassManager passManager,
84+
MlirStringRef outputDir);
85+
8186
/// Enable / disable verify-each.
8287
MLIR_CAPI_EXPORTED void
8388
mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable);

mlir/include/mlir/Conversion/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "mlir/Conversion/AMDGPUToROCDL/AMDGPUToROCDL.h"
1313
#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
1414
#include "mlir/Conversion/ArithToAMDGPU/ArithToAMDGPU.h"
15-
#include "mlir/Conversion/ArithToEmitC/ArithToEmitCPass.h"
1615
#include "mlir/Conversion/ArithToArmSME/ArithToArmSME.h"
1716
#include "mlir/Conversion/ArithToEmitC/ArithToEmitCPass.h"
1817
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
@@ -72,6 +71,7 @@
7271
#include "mlir/Conversion/TosaToMLProgram/TosaToMLProgram.h"
7372
#include "mlir/Conversion/TosaToSCF/TosaToSCF.h"
7473
#include "mlir/Conversion/TosaToTensor/TosaToTensor.h"
74+
#include "mlir/Conversion/UBToEmitC/UBToEmitC.h"
7575
#include "mlir/Conversion/UBToLLVM/UBToLLVM.h"
7676
#include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h"
7777
#include "mlir/Conversion/VectorToArmSME/VectorToArmSME.h"

mlir/include/mlir/Conversion/Passes.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,6 +1238,23 @@ def TosaToTensor : Pass<"tosa-to-tensor"> {
12381238
let constructor = "tosa::createTosaToTensor()";
12391239
}
12401240

1241+
//===----------------------------------------------------------------------===//
1242+
// UBToEmitC
1243+
//===----------------------------------------------------------------------===//
1244+
1245+
def ConvertUBToEmitC : Pass<"convert-ub-to-emitc"> {
1246+
let summary = "Convert UB dialect to EmitC dialect";
1247+
let description = [{
1248+
This pass converts supported UB ops to EmitC dialect.
1249+
}];
1250+
let dependentDialects = ["emitc::EmitCDialect"];
1251+
let options = [
1252+
Option<"noInitialization", "no-initialization", "bool",
1253+
/*default=*/"false",
1254+
"Do not initialize the generated variables">,
1255+
];
1256+
}
1257+
12411258
//===----------------------------------------------------------------------===//
12421259
// UBToLLVM
12431260
//===----------------------------------------------------------------------===//
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===- UBToEmitC.h - UB to EmitC dialect conversion -------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H
10+
#define MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H
11+
12+
#include "mlir/Pass/Pass.h"
13+
#include "mlir/Transforms/DialectConversion.h"
14+
15+
namespace mlir {
16+
#define GEN_PASS_DECL_CONVERTUBTOEMITC
17+
#include "mlir/Conversion/Passes.h.inc"
18+
19+
namespace ub {
20+
void populateUBToEmitCConversionPatterns(TypeConverter &converter,
21+
RewritePatternSet &patterns,
22+
bool noInitialization);
23+
} // namespace ub
24+
} // namespace mlir
25+
26+
#endif // MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H

mlir/include/mlir/Dialect/Affine/IR/AffineOps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ def AffineForOp : Affine_Op<"for",
336336
def AffineIfOp : Affine_Op<"if",
337337
[ImplicitAffineTerminator, RecursivelySpeculatable,
338338
RecursiveMemoryEffects, NoRegionArguments,
339-
DeclareOpInterfaceMethods<RegionBranchOpInterface>
339+
DeclareOpInterfaceMethods<RegionBranchOpInterface,
340+
["getRegionInvocationBounds"]>
340341
]> {
341342
let summary = "if-then-else operation";
342343
let description = [{

mlir/include/mlir/Dialect/Affine/Utils.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class FuncOp;
3131

3232
namespace memref {
3333
class AllocOp;
34+
class AllocaOp;
3435
} // namespace memref
3536

3637
namespace affine {
@@ -245,7 +246,12 @@ LogicalResult replaceAllMemRefUsesWith(Value oldMemRef, Value newMemRef,
245246
/// Rewrites the memref defined by this alloc op to have an identity layout map
246247
/// and updates all its indexing uses. Returns failure if any of its uses
247248
/// escape (while leaving the IR in a valid state).
248-
LogicalResult normalizeMemRef(memref::AllocOp *op);
249+
template <typename AllocLikeOp>
250+
LogicalResult normalizeMemRef(AllocLikeOp *op);
251+
extern template LogicalResult
252+
normalizeMemRef<memref::AllocaOp>(memref::AllocaOp *op);
253+
extern template LogicalResult
254+
normalizeMemRef<memref::AllocOp>(memref::AllocOp *op);
249255

250256
/// Normalizes `memrefType` so that the affine layout map of the memref is
251257
/// transformed to an identity map with a new shape being computed for the

mlir/include/mlir/Dialect/EmitC/IR/EmitC.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "mlir/Dialect/EmitC/IR/EmitCDialect.h.inc"
2828
#include "mlir/Dialect/EmitC/IR/EmitCEnums.h.inc"
2929

30+
#include <variant>
31+
3032
namespace mlir {
3133
namespace emitc {
3234
void buildTerminatedBody(OpBuilder &builder, Location loc);
@@ -47,6 +49,13 @@ bool isSupportedFloatType(mlir::Type type);
4749
/// Determines whether \p type is a emitc.size_t/ssize_t type.
4850
bool isPointerWideType(mlir::Type type);
4951

52+
/// Give the name of the EmitC reference attribute.
53+
StringRef getReferenceAttributeName();
54+
55+
// Either a literal string, or an placeholder for the fmtArgs.
56+
struct Placeholder {};
57+
using ReplacementItem = std::variant<StringRef, Placeholder>;
58+
5059
} // namespace emitc
5160
} // namespace mlir
5261

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ include "mlir/Interfaces/CastInterfaces.td"
2121
include "mlir/Interfaces/ControlFlowInterfaces.td"
2222
include "mlir/Interfaces/FunctionInterfaces.td"
2323
include "mlir/Interfaces/SideEffectInterfaces.td"
24+
include "mlir/IR/OpAsmInterface.td"
2425
include "mlir/IR/RegionKindInterface.td"
26+
include "mlir/IR/BuiltinAttributes.td"
2527

2628
//===----------------------------------------------------------------------===//
2729
// EmitC op definitions
@@ -55,6 +57,55 @@ def IntegerIndexOrOpaqueType : Type<CPred<"emitc::isIntegerIndexOrOpaqueType($_s
5557
"integer, index or opaque type supported by EmitC">;
5658
def FloatIntegerIndexOrOpaqueType : AnyTypeOf<[EmitCFloatType, IntegerIndexOrOpaqueType]>;
5759

60+
def EmitC_TranslationUnitOp : EmitC_Op<"tu",
61+
[IsolatedFromAbove, NoRegionArguments, SymbolTable,
62+
OpAsmOpInterface
63+
] # GraphRegionNoTerminator.traits> {
64+
let summary = "A translation unit container operation";
65+
let description = [{
66+
A `tu` represents a translation unit that can be emitted
67+
into a single C++ file.
68+
69+
`mlir-translate` emits only the translation unit selected via
70+
the `-translation-unit-id=id` flag. By default, no translation units are
71+
emitted.
72+
73+
Example:
74+
75+
```mlir
76+
emitc.tu "main" {
77+
emitc.func @func_one() {
78+
emitc.return
79+
}
80+
}
81+
```
82+
}];
83+
84+
let arguments = (ins Builtin_StringAttr:$id);
85+
let regions = (region SizedRegion<1>:$bodyRegion);
86+
87+
let assemblyFormat = "$id attr-dict-with-keyword $bodyRegion";
88+
let builders = [OpBuilder<(ins CArg<"StringRef">:$id)>];
89+
let extraClassDeclaration = [{
90+
/// Construct a module from the given location with an optional name.
91+
static TranslationUnitOp create(Location loc, StringRef name);
92+
93+
//===------------------------------------------------------------------===//
94+
// OpAsmOpInterface Methods
95+
//===------------------------------------------------------------------===//
96+
97+
/// EmitC ops in the body of the translation_unit can omit their 'emitc.'
98+
/// prefix in the assembly.
99+
static ::llvm::StringRef getDefaultDialect() {
100+
return "emitc";
101+
}
102+
}];
103+
104+
// We need to ensure that the body region has a block;
105+
// the auto-generated builders do not guarantee that.
106+
let skipDefaultBuilders = 1;
107+
}
108+
58109
def EmitC_AddOp : EmitC_BinaryOp<"add", [CExpression]> {
59110
let summary = "Addition operation";
60111
let description = [{
@@ -241,6 +292,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {
241292
Arg<StrAttr, "the C++ function to call">:$callee,
242293
Arg<OptionalAttr<ArrayAttr>, "the order of operands and further attributes">:$args,
243294
Arg<OptionalAttr<ArrayAttr>, "template arguments">:$template_args,
295+
Arg<OptionalAttr<StrArrayAttr>, "template argument names">:$template_arg_names,
244296
Variadic<EmitCType>:$operands
245297
);
246298
let results = (outs Variadic<EmitCType>);
@@ -251,7 +303,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {
251303
"::mlir::ValueRange":$operands,
252304
CArg<"::mlir::ArrayAttr", "{}">:$args,
253305
CArg<"::mlir::ArrayAttr", "{}">:$template_args), [{
254-
build($_builder, $_state, resultTypes, callee, args, template_args,
306+
build($_builder, $_state, resultTypes, callee, args, template_args, {},
255307
operands);
256308
}]
257309
>
@@ -265,8 +317,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {
265317

266318
def EmitC_CastOp : EmitC_Op<"cast",
267319
[CExpression,
268-
DeclareOpInterfaceMethods<CastOpInterface>,
269-
SameOperandsAndResultShape]> {
320+
DeclareOpInterfaceMethods<CastOpInterface>]> {
270321
let summary = "Cast operation";
271322
let description = [{
272323
The `emitc.cast` operation performs an explicit type conversion and is emitted
@@ -285,9 +336,11 @@ def EmitC_CastOp : EmitC_Op<"cast",
285336
```
286337
}];
287338

288-
let arguments = (ins EmitCType:$source);
339+
let arguments = (ins EmitCType:$source,
340+
UnitAttr:$reference);
289341
let results = (outs EmitCType:$dest);
290-
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest)";
342+
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest) (`ref` $reference^)?";
343+
let hasVerifier = 1;
291344
}
292345

293346
def EmitC_CmpOp : EmitC_BinaryOp<"cmp", [CExpression]> {
@@ -700,6 +753,12 @@ def EmitC_FuncOp : EmitC_Op<"func", [
700753

701754
/// Returns the result types of this function.
702755
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }
756+
757+
//===------------------------------------------------------------------===//
758+
// SymbolOpInterface Methods
759+
//===------------------------------------------------------------------===//
760+
761+
bool isDeclaration() { return isExternal(); }
703762
}];
704763
let hasCustomAssemblyFormat = 1;
705764
let hasVerifier = 1;
@@ -730,7 +789,7 @@ def EmitC_ReturnOp : EmitC_Op<"return", [Pure, HasParent<"FuncOp">,
730789
}
731790

732791
def EmitC_IncludeOp
733-
: EmitC_Op<"include", [HasParent<"ModuleOp">]> {
792+
: EmitC_Op<"include", []> {
734793
let summary = "Include operation";
735794
let description = [{
736795
The `emitc.include` operation allows to define a source file inclusion via the
@@ -1092,14 +1151,16 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
10921151
OptionalAttr<EmitC_OpaqueOrTypedAttr>:$initial_value,
10931152
UnitAttr:$extern_specifier,
10941153
UnitAttr:$static_specifier,
1095-
UnitAttr:$const_specifier);
1154+
UnitAttr:$const_specifier,
1155+
UnitAttr:$reference);
10961156

10971157
let assemblyFormat = [{
10981158
(`extern` $extern_specifier^)?
10991159
(`static` $static_specifier^)?
11001160
(`const` $const_specifier^)?
11011161
$sym_name
11021162
`:` custom<EmitCGlobalOpTypeAndInitialValue>($type, $initial_value)
1163+
(`ref` $reference^)?
11031164
attr-dict
11041165
}];
11051166

@@ -1154,10 +1215,27 @@ def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
11541215
}
11551216
#endif
11561217
```
1218+
1219+
If the `emitc.verbatim` op has operands, then the `value` is interpreted as
1220+
format string, where `{}` is a placeholder for an operand in their order.
1221+
For example, `emitc.verbatim "#pragma my src={} dst={}" %src, %dest : i32, i32`
1222+
would be emitted as `#pragma my src=a dst=b` if `%src` became `a` and
1223+
`%dest` became `b` in the C code.
1224+
`{{` in the format string is interpreted as a single `{` and doesn't introduce
1225+
a placeholder.
11571226
}];
11581227

1159-
let arguments = (ins StrAttr:$value);
1160-
let assemblyFormat = "$value attr-dict";
1228+
let extraClassDeclaration = [{
1229+
FailureOr<SmallVector<::mlir::emitc::ReplacementItem>> parseFormatString();
1230+
}];
1231+
1232+
let arguments = (ins StrAttr:$value,
1233+
Variadic<EmitCType>:$fmtArgs);
1234+
1235+
let builders = [OpBuilder<(ins "::mlir::StringAttr":$value), [{ build($_builder, $_state, value, {}); }] >];
1236+
let builders = [OpBuilder<(ins "::llvm::StringRef":$value), [{ build($_builder, $_state, value, {}); }] >];
1237+
let hasVerifier = 1;
1238+
let assemblyFormat = "$value (`args` $fmtArgs^ `:` type($fmtArgs))? attr-dict";
11611239
}
11621240

11631241
def EmitC_AssignOp : EmitC_Op<"assign", []> {

mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,16 @@ def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
9999
```
100100
}];
101101

102-
let parameters = (ins StringRefParameter<"the opaque value">:$value);
103-
let assemblyFormat = "`<` $value `>`";
102+
let parameters = (ins StringRefParameter<"the opaque value">:$value,
103+
OptionalArrayRefParameter<"Type">:$fmtArgs);
104+
let assemblyFormat = "`<` $value (`,` custom<VariadicTypeFmtArgs>($fmtArgs)^)? `>`";
104105
let genVerifyDecl = 1;
106+
107+
let builders = [TypeBuilder<(ins "::llvm::StringRef":$value), [{ return $_get($_ctxt, value, SmallVector<Type>{}); }] >];
108+
109+
let extraClassDeclaration = [{
110+
FailureOr<SmallVector<::mlir::emitc::ReplacementItem>> parseFormatString();
111+
}];
105112
}
106113

107114
def EmitC_PointerType : EmitC_Type<"Pointer", "ptr"> {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===---------- FunctionOpAssembly.h - Parser for `emitc.func` op ---------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef MLIR_INCLUDE_MLIR_DIALECT_EMITC_IR_FUNCTIONOPASSEMBLY_H
10+
#define MLIR_INCLUDE_MLIR_DIALECT_EMITC_IR_FUNCTIONOPASSEMBLY_H
11+
12+
#include "mlir/IR/OperationSupport.h"
13+
#include "mlir/Interfaces/FunctionImplementation.h"
14+
#include "mlir/Support/LogicalResult.h"
15+
16+
#include "mlir/IR/Builders.h"
17+
18+
namespace mlir::emitc {
19+
20+
class FuncOp;
21+
22+
ParseResult
23+
parseFunctionSignature(OpAsmParser &parser, bool allowVariadic,
24+
SmallVectorImpl<OpAsmParser::Argument> &arguments,
25+
bool &isVariadic, SmallVectorImpl<Type> &resultTypes,
26+
SmallVectorImpl<DictionaryAttr> &resultAttrs);
27+
28+
ParseResult
29+
parseFunctionOp(OpAsmParser &parser, OperationState &result, bool allowVariadic,
30+
StringAttr typeAttrName,
31+
function_interface_impl::FuncTypeBuilder funcTypeBuilder,
32+
StringAttr argAttrsName, StringAttr resAttrsName);
33+
34+
void printFunctionSignature(OpAsmPrinter &p, FuncOp op, ArrayRef<Type> argTypes,
35+
bool isVariadic, ArrayRef<Type> resultTypes);
36+
37+
void printFunctionOp(OpAsmPrinter &p, FuncOp op, bool isVariadic,
38+
StringRef typeAttrName, StringAttr argAttrsName,
39+
StringAttr resAttrsName);
40+
41+
} // namespace mlir::emitc
42+
43+
#endif // MLIR_INCLUDE_MLIR_DIALECT_EMITC_IR_FUNCTIONOPASSEMBLY_H

mlir/include/mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,23 @@ def FuseOp : Op<Transform_Dialect, "structured.fuse",
284284
let description = [{
285285
Tiles the operations pointed to by the target handle and fuses their
286286
producers greedily using the options provided as attributes.
287+
288+
If `apply_cleanup` is true then slice canonicalization is applied between
289+
fusion steps.
287290
}];
288291

289292
let arguments =
290293
(ins TransformHandleTypeInterface:$target,
291294
DefaultValuedAttr<I64ArrayAttr, "{}">:$tile_sizes,
292-
DefaultValuedAttr<I64ArrayAttr, "{}">:$tile_interchange);
295+
DefaultValuedAttr<I64ArrayAttr, "{}">:$tile_interchange,
296+
DefaultValuedAttr<BoolAttr, "false">:$apply_cleanup);
293297
let results = (outs TransformHandleTypeInterface:$transformed,
294298
Variadic<TransformHandleTypeInterface>:$loops);
295299

296300
let assemblyFormat = [{
297301
$target ($tile_sizes^)? (`interchange` $tile_interchange^)?
298-
attr-dict `:` functional-type(operands, results)
302+
(`apply_cleanup` `=` $apply_cleanup^)? attr-dict
303+
`:` functional-type(operands, results)
299304
}];
300305
let hasVerifier = 1;
301306
}

0 commit comments

Comments
 (0)