Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/fused-ops' into bump_to_…
Browse files Browse the repository at this point in the history
…2d50029f
  • Loading branch information
mgehre-amd committed Nov 29, 2024
2 parents 6f28929 + 19bdfae commit 7d54e5d
Show file tree
Hide file tree
Showing 85 changed files with 3,141 additions and 247 deletions.
5 changes: 5 additions & 0 deletions mlir/include/mlir-c/Pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ mlirPassManagerRunOnOp(MlirPassManager passManager, MlirOperation op);
MLIR_CAPI_EXPORTED void
mlirPassManagerEnableIRPrinting(MlirPassManager passManager);

/// Enable lir-reproducer-before-all.
MLIR_CAPI_EXPORTED void
mlirPassManagerEnableReproducerBeforeAll(MlirPassManager passManager,
MlirStringRef outputDir);

/// Enable / disable verify-each.
MLIR_CAPI_EXPORTED void
mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable);
Expand Down
2 changes: 1 addition & 1 deletion mlir/include/mlir/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "mlir/Conversion/AMDGPUToROCDL/AMDGPUToROCDL.h"
#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/ArithToAMDGPU/ArithToAMDGPU.h"
#include "mlir/Conversion/ArithToEmitC/ArithToEmitCPass.h"
#include "mlir/Conversion/ArithToArmSME/ArithToArmSME.h"
#include "mlir/Conversion/ArithToEmitC/ArithToEmitCPass.h"
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
Expand Down Expand Up @@ -72,6 +71,7 @@
#include "mlir/Conversion/TosaToMLProgram/TosaToMLProgram.h"
#include "mlir/Conversion/TosaToSCF/TosaToSCF.h"
#include "mlir/Conversion/TosaToTensor/TosaToTensor.h"
#include "mlir/Conversion/UBToEmitC/UBToEmitC.h"
#include "mlir/Conversion/UBToLLVM/UBToLLVM.h"
#include "mlir/Conversion/UBToSPIRV/UBToSPIRV.h"
#include "mlir/Conversion/VectorToArmSME/VectorToArmSME.h"
Expand Down
17 changes: 17 additions & 0 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,23 @@ def TosaToTensor : Pass<"tosa-to-tensor"> {
let constructor = "tosa::createTosaToTensor()";
}

//===----------------------------------------------------------------------===//
// UBToEmitC
//===----------------------------------------------------------------------===//

def ConvertUBToEmitC : Pass<"convert-ub-to-emitc"> {
let summary = "Convert UB dialect to EmitC dialect";
let description = [{
This pass converts supported UB ops to EmitC dialect.
}];
let dependentDialects = ["emitc::EmitCDialect"];
let options = [
Option<"noInitialization", "no-initialization", "bool",
/*default=*/"false",
"Do not initialize the generated variables">,
];
}

//===----------------------------------------------------------------------===//
// UBToLLVM
//===----------------------------------------------------------------------===//
Expand Down
26 changes: 26 additions & 0 deletions mlir/include/mlir/Conversion/UBToEmitC/UBToEmitC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===- UBToEmitC.h - UB to EmitC dialect conversion -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H
#define MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H

#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"

namespace mlir {
#define GEN_PASS_DECL_CONVERTUBTOEMITC
#include "mlir/Conversion/Passes.h.inc"

namespace ub {
void populateUBToEmitCConversionPatterns(TypeConverter &converter,
RewritePatternSet &patterns,
bool noInitialization);
} // namespace ub
} // namespace mlir

#endif // MLIR_CONVERSION_UBTOEMITC_UBTOEMITC_H
3 changes: 2 additions & 1 deletion mlir/include/mlir/Dialect/Affine/IR/AffineOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ def AffineForOp : Affine_Op<"for",
def AffineIfOp : Affine_Op<"if",
[ImplicitAffineTerminator, RecursivelySpeculatable,
RecursiveMemoryEffects, NoRegionArguments,
DeclareOpInterfaceMethods<RegionBranchOpInterface>
DeclareOpInterfaceMethods<RegionBranchOpInterface,
["getRegionInvocationBounds"]>
]> {
let summary = "if-then-else operation";
let description = [{
Expand Down
8 changes: 7 additions & 1 deletion mlir/include/mlir/Dialect/Affine/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class FuncOp;

namespace memref {
class AllocOp;
class AllocaOp;
} // namespace memref

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

/// Normalizes `memrefType` so that the affine layout map of the memref is
/// transformed to an identity map with a new shape being computed for the
Expand Down
9 changes: 9 additions & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "mlir/Dialect/EmitC/IR/EmitCDialect.h.inc"
#include "mlir/Dialect/EmitC/IR/EmitCEnums.h.inc"

#include <variant>

namespace mlir {
namespace emitc {
void buildTerminatedBody(OpBuilder &builder, Location loc);
Expand All @@ -47,6 +49,13 @@ bool isSupportedFloatType(mlir::Type type);
/// Determines whether \p type is a emitc.size_t/ssize_t type.
bool isPointerWideType(mlir::Type type);

/// Give the name of the EmitC reference attribute.
StringRef getReferenceAttributeName();

// Either a literal string, or an placeholder for the fmtArgs.
struct Placeholder {};
using ReplacementItem = std::variant<StringRef, Placeholder>;

} // namespace emitc
} // namespace mlir

Expand Down
96 changes: 87 additions & 9 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ include "mlir/Interfaces/CastInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/RegionKindInterface.td"
include "mlir/IR/BuiltinAttributes.td"

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

def EmitC_TranslationUnitOp : EmitC_Op<"tu",
[IsolatedFromAbove, NoRegionArguments, SymbolTable,
OpAsmOpInterface
] # GraphRegionNoTerminator.traits> {
let summary = "A translation unit container operation";
let description = [{
A `tu` represents a translation unit that can be emitted
into a single C++ file.

`mlir-translate` emits only the translation unit selected via
the `-translation-unit-id=id` flag. By default, no translation units are
emitted.

Example:

```mlir
emitc.tu "main" {
emitc.func @func_one() {
emitc.return
}
}
```
}];

let arguments = (ins Builtin_StringAttr:$id);
let regions = (region SizedRegion<1>:$bodyRegion);

let assemblyFormat = "$id attr-dict-with-keyword $bodyRegion";
let builders = [OpBuilder<(ins CArg<"StringRef">:$id)>];
let extraClassDeclaration = [{
/// Construct a module from the given location with an optional name.
static TranslationUnitOp create(Location loc, StringRef name);

//===------------------------------------------------------------------===//
// OpAsmOpInterface Methods
//===------------------------------------------------------------------===//

/// EmitC ops in the body of the translation_unit can omit their 'emitc.'
/// prefix in the assembly.
static ::llvm::StringRef getDefaultDialect() {
return "emitc";
}
}];

// We need to ensure that the body region has a block;
// the auto-generated builders do not guarantee that.
let skipDefaultBuilders = 1;
}

def EmitC_AddOp : EmitC_BinaryOp<"add", [CExpression]> {
let summary = "Addition operation";
let description = [{
Expand Down Expand Up @@ -241,6 +292,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {
Arg<StrAttr, "the C++ function to call">:$callee,
Arg<OptionalAttr<ArrayAttr>, "the order of operands and further attributes">:$args,
Arg<OptionalAttr<ArrayAttr>, "template arguments">:$template_args,
Arg<OptionalAttr<StrArrayAttr>, "template argument names">:$template_arg_names,
Variadic<EmitCType>:$operands
);
let results = (outs Variadic<EmitCType>);
Expand All @@ -251,7 +303,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {
"::mlir::ValueRange":$operands,
CArg<"::mlir::ArrayAttr", "{}">:$args,
CArg<"::mlir::ArrayAttr", "{}">:$template_args), [{
build($_builder, $_state, resultTypes, callee, args, template_args,
build($_builder, $_state, resultTypes, callee, args, template_args, {},
operands);
}]
>
Expand All @@ -265,8 +317,7 @@ def EmitC_CallOpaqueOp : EmitC_Op<"call_opaque", [CExpression]> {

def EmitC_CastOp : EmitC_Op<"cast",
[CExpression,
DeclareOpInterfaceMethods<CastOpInterface>,
SameOperandsAndResultShape]> {
DeclareOpInterfaceMethods<CastOpInterface>]> {
let summary = "Cast operation";
let description = [{
The `emitc.cast` operation performs an explicit type conversion and is emitted
Expand All @@ -285,9 +336,11 @@ def EmitC_CastOp : EmitC_Op<"cast",
```
}];

let arguments = (ins EmitCType:$source);
let arguments = (ins EmitCType:$source,
UnitAttr:$reference);
let results = (outs EmitCType:$dest);
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest)";
let assemblyFormat = "$source attr-dict `:` type($source) `to` type($dest) (`ref` $reference^)?";
let hasVerifier = 1;
}

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

/// Returns the result types of this function.
ArrayRef<Type> getResultTypes() { return getFunctionType().getResults(); }

//===------------------------------------------------------------------===//
// SymbolOpInterface Methods
//===------------------------------------------------------------------===//

bool isDeclaration() { return isExternal(); }
}];
let hasCustomAssemblyFormat = 1;
let hasVerifier = 1;
Expand Down Expand Up @@ -730,7 +789,7 @@ def EmitC_ReturnOp : EmitC_Op<"return", [Pure, HasParent<"FuncOp">,
}

def EmitC_IncludeOp
: EmitC_Op<"include", [HasParent<"ModuleOp">]> {
: EmitC_Op<"include", []> {
let summary = "Include operation";
let description = [{
The `emitc.include` operation allows to define a source file inclusion via the
Expand Down Expand Up @@ -1092,14 +1151,16 @@ def EmitC_GlobalOp : EmitC_Op<"global", [Symbol]> {
OptionalAttr<EmitC_OpaqueOrTypedAttr>:$initial_value,
UnitAttr:$extern_specifier,
UnitAttr:$static_specifier,
UnitAttr:$const_specifier);
UnitAttr:$const_specifier,
UnitAttr:$reference);

let assemblyFormat = [{
(`extern` $extern_specifier^)?
(`static` $static_specifier^)?
(`const` $const_specifier^)?
$sym_name
`:` custom<EmitCGlobalOpTypeAndInitialValue>($type, $initial_value)
(`ref` $reference^)?
attr-dict
}];

Expand Down Expand Up @@ -1154,10 +1215,27 @@ def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
}
#endif
```

If the `emitc.verbatim` op has operands, then the `value` is interpreted as
format string, where `{}` is a placeholder for an operand in their order.
For example, `emitc.verbatim "#pragma my src={} dst={}" %src, %dest : i32, i32`
would be emitted as `#pragma my src=a dst=b` if `%src` became `a` and
`%dest` became `b` in the C code.
`{{` in the format string is interpreted as a single `{` and doesn't introduce
a placeholder.
}];

let arguments = (ins StrAttr:$value);
let assemblyFormat = "$value attr-dict";
let extraClassDeclaration = [{
FailureOr<SmallVector<::mlir::emitc::ReplacementItem>> parseFormatString();
}];

let arguments = (ins StrAttr:$value,
Variadic<EmitCType>:$fmtArgs);

let builders = [OpBuilder<(ins "::mlir::StringAttr":$value), [{ build($_builder, $_state, value, {}); }] >];
let builders = [OpBuilder<(ins "::llvm::StringRef":$value), [{ build($_builder, $_state, value, {}); }] >];
let hasVerifier = 1;
let assemblyFormat = "$value (`args` $fmtArgs^ `:` type($fmtArgs))? attr-dict";
}

def EmitC_AssignOp : EmitC_Op<"assign", []> {
Expand Down
11 changes: 9 additions & 2 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,16 @@ def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
```
}];

let parameters = (ins StringRefParameter<"the opaque value">:$value);
let assemblyFormat = "`<` $value `>`";
let parameters = (ins StringRefParameter<"the opaque value">:$value,
OptionalArrayRefParameter<"Type">:$fmtArgs);
let assemblyFormat = "`<` $value (`,` custom<VariadicTypeFmtArgs>($fmtArgs)^)? `>`";
let genVerifyDecl = 1;

let builders = [TypeBuilder<(ins "::llvm::StringRef":$value), [{ return $_get($_ctxt, value, SmallVector<Type>{}); }] >];

let extraClassDeclaration = [{
FailureOr<SmallVector<::mlir::emitc::ReplacementItem>> parseFormatString();
}];
}

def EmitC_PointerType : EmitC_Type<"Pointer", "ptr"> {
Expand Down
43 changes: 43 additions & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/FunctionOpAssembly.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===---------- FunctionOpAssembly.h - Parser for `emitc.func` op ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_INCLUDE_MLIR_DIALECT_EMITC_IR_FUNCTIONOPASSEMBLY_H
#define MLIR_INCLUDE_MLIR_DIALECT_EMITC_IR_FUNCTIONOPASSEMBLY_H

#include "mlir/IR/OperationSupport.h"
#include "mlir/Interfaces/FunctionImplementation.h"
#include "mlir/Support/LogicalResult.h"

#include "mlir/IR/Builders.h"

namespace mlir::emitc {

class FuncOp;

ParseResult
parseFunctionSignature(OpAsmParser &parser, bool allowVariadic,
SmallVectorImpl<OpAsmParser::Argument> &arguments,
bool &isVariadic, SmallVectorImpl<Type> &resultTypes,
SmallVectorImpl<DictionaryAttr> &resultAttrs);

ParseResult
parseFunctionOp(OpAsmParser &parser, OperationState &result, bool allowVariadic,
StringAttr typeAttrName,
function_interface_impl::FuncTypeBuilder funcTypeBuilder,
StringAttr argAttrsName, StringAttr resAttrsName);

void printFunctionSignature(OpAsmPrinter &p, FuncOp op, ArrayRef<Type> argTypes,
bool isVariadic, ArrayRef<Type> resultTypes);

void printFunctionOp(OpAsmPrinter &p, FuncOp op, bool isVariadic,
StringRef typeAttrName, StringAttr argAttrsName,
StringAttr resAttrsName);

} // namespace mlir::emitc

#endif // MLIR_INCLUDE_MLIR_DIALECT_EMITC_IR_FUNCTIONOPASSEMBLY_H
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,23 @@ def FuseOp : Op<Transform_Dialect, "structured.fuse",
let description = [{
Tiles the operations pointed to by the target handle and fuses their
producers greedily using the options provided as attributes.

If `apply_cleanup` is true then slice canonicalization is applied between
fusion steps.
}];

let arguments =
(ins TransformHandleTypeInterface:$target,
DefaultValuedAttr<I64ArrayAttr, "{}">:$tile_sizes,
DefaultValuedAttr<I64ArrayAttr, "{}">:$tile_interchange);
DefaultValuedAttr<I64ArrayAttr, "{}">:$tile_interchange,
DefaultValuedAttr<BoolAttr, "false">:$apply_cleanup);
let results = (outs TransformHandleTypeInterface:$transformed,
Variadic<TransformHandleTypeInterface>:$loops);

let assemblyFormat = [{
$target ($tile_sizes^)? (`interchange` $tile_interchange^)?
attr-dict `:` functional-type(operands, results)
(`apply_cleanup` `=` $apply_cleanup^)? attr-dict
`:` functional-type(operands, results)
}];
let hasVerifier = 1;
}
Expand Down
Loading

0 comments on commit 7d54e5d

Please sign in to comment.