From bc23d58199bac96769e00d35c9a0567f692f34ae Mon Sep 17 00:00:00 2001 From: Alwaysproblem Date: Thu, 8 Jun 2023 05:58:08 +0000 Subject: [PATCH] support 16.x --- mlir/example/Ch1/include/toy/AST.h | 9 +++--- mlir/example/Ch1/include/toy/Parser.h | 4 +-- mlir/example/Ch2/include/toy/AST.h | 9 +++--- mlir/example/Ch2/include/toy/Ops.td | 16 +++++----- mlir/example/Ch2/include/toy/Parser.h | 4 +-- mlir/example/Ch2/mlir/Dialect.cpp | 9 ++++-- mlir/example/Ch2/mlir/MLIRGen.cpp | 11 ++++--- mlir/example/Ch3/include/toy/AST.h | 9 +++--- mlir/example/Ch3/include/toy/Ops.td | 24 ++++++++------- mlir/example/Ch3/include/toy/Parser.h | 4 +-- mlir/example/Ch3/mlir/Dialect.cpp | 9 ++++-- mlir/example/Ch3/mlir/MLIRGen.cpp | 11 ++++--- mlir/example/Ch4/include/toy/AST.h | 9 +++--- mlir/example/Ch4/include/toy/Ops.td | 26 ++++++++-------- mlir/example/Ch4/include/toy/Parser.h | 4 +-- mlir/example/Ch4/mlir/Dialect.cpp | 15 +++++----- mlir/example/Ch4/mlir/MLIRGen.cpp | 11 ++++--- mlir/example/Ch5/include/toy/AST.h | 9 +++--- mlir/example/Ch5/include/toy/Ops.td | 26 ++++++++-------- mlir/example/Ch5/include/toy/Parser.h | 4 +-- mlir/example/Ch5/mlir/Dialect.cpp | 15 +++++----- mlir/example/Ch5/mlir/LowerToAffineLoops.cpp | 11 ++++--- mlir/example/Ch5/mlir/MLIRGen.cpp | 11 ++++--- mlir/example/Ch6/include/toy/AST.h | 9 +++--- mlir/example/Ch6/include/toy/Ops.td | 26 ++++++++-------- mlir/example/Ch6/include/toy/Parser.h | 4 +-- mlir/example/Ch6/mlir/Dialect.cpp | 15 +++++----- mlir/example/Ch6/mlir/LowerToAffineLoops.cpp | 11 ++++--- mlir/example/Ch6/mlir/LowerToLLVM.cpp | 12 ++++---- mlir/example/Ch6/mlir/MLIRGen.cpp | 11 ++++--- mlir/example/Ch7/include/toy/AST.h | 9 +++--- mlir/example/Ch7/include/toy/Ops.td | 31 +++++++++++--------- mlir/example/Ch7/include/toy/Parser.h | 4 +-- mlir/example/Ch7/mlir/Dialect.cpp | 15 +++++----- mlir/example/Ch7/mlir/LowerToAffineLoops.cpp | 11 ++++--- mlir/example/Ch7/mlir/LowerToLLVM.cpp | 12 ++++---- mlir/example/Ch7/mlir/MLIRGen.cpp | 28 +++++++++--------- mlir/example/Ch7/mlir/ToyCombine.cpp | 12 +++----- mlir/example/README.md | 2 +- 39 files changed, 243 insertions(+), 229 deletions(-) diff --git a/mlir/example/Ch1/include/toy/AST.h b/mlir/example/Ch1/include/toy/AST.h index e41370b..c9d1bdb 100644 --- a/mlir/example/Ch1/include/toy/AST.h +++ b/mlir/example/Ch1/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -131,15 +132,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch1/include/toy/Parser.h b/mlir/example/Ch1/include/toy/Parser.h index 9b19077..ededa4c 100644 --- a/mlir/example/Ch1/include/toy/Parser.h +++ b/mlir/example/Ch1/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -64,7 +64,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch2/include/toy/AST.h b/mlir/example/Ch2/include/toy/AST.h index e41370b..c9d1bdb 100644 --- a/mlir/example/Ch2/include/toy/AST.h +++ b/mlir/example/Ch2/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -131,15 +132,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch2/include/toy/Ops.td b/mlir/example/Ch2/include/toy/Ops.td index e3e920e..ffe56e2 100644 --- a/mlir/example/Ch2/include/toy/Ops.td +++ b/mlir/example/Ch2/include/toy/Ops.td @@ -23,7 +23,7 @@ include "mlir/Interfaces/SideEffectInterfaces.td" def Toy_Dialect : Dialect { let name = "toy"; let cppNamespace = "::mlir::toy"; - let emitAccessorPrefix = kEmitAccessorPrefix_Prefixed; + let useFoldAPI = kEmitFoldAdaptorFolder; } // Base class for toy dialect operations. This operation inherits from the base @@ -44,9 +44,9 @@ class Toy_Op traits = []> : // We define a toy operation by inheriting from our base 'Toy_Op' class above. // Here we provide the mnemonic and a list of traits for the operation. The -// constant operation is marked as 'NoSideEffect' as it is a pure operation +// constant operation is marked as 'Pure' as it is a pure operation // and may be removed if dead. -def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { +def ConstantOp : Toy_Op<"constant", [Pure]> { // Provide a summary and description for this operation. This can be used to // auto-generate documentation of the operations within our dialect. let summary = "constant"; @@ -114,7 +114,7 @@ def AddOp : Toy_Op<"add"> { //===----------------------------------------------------------------------===// def FuncOp : Toy_Op<"func", [ - FunctionOpInterface, IsolatedFromAbove, Symbol + FunctionOpInterface, IsolatedFromAbove ]> { let summary = "user defined function operation"; let description = [{ @@ -135,7 +135,9 @@ def FuncOp : Toy_Op<"func", [ let arguments = (ins SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type + TypeAttrOf:$function_type, + OptionalAttr:$arg_attrs, + OptionalAttr:$res_attrs ); let regions = (region AnyRegion:$body); @@ -266,7 +268,7 @@ def ReshapeOp : Toy_Op<"reshape"> { // ReturnOp //===----------------------------------------------------------------------===// -def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, +def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let summary = "return operation"; let description = [{ @@ -292,7 +294,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // Allow building a ReturnOp with no return operand. let builders = [ - OpBuilder<(ins), [{ build($_builder, $_state, llvm::None); }]> + OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]> ]; // Provide extra utility definitions on the c++ operation class definition. diff --git a/mlir/example/Ch2/include/toy/Parser.h b/mlir/example/Ch2/include/toy/Parser.h index 9b19077..ededa4c 100644 --- a/mlir/example/Ch2/include/toy/Parser.h +++ b/mlir/example/Ch2/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -64,7 +64,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch2/mlir/Dialect.cpp b/mlir/example/Ch2/mlir/Dialect.cpp index ffb03dd..3d9af10 100644 --- a/mlir/example/Ch2/mlir/Dialect.cpp +++ b/mlir/example/Ch2/mlir/Dialect.cpp @@ -211,14 +211,17 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, std::string &) { return builder.getFunctionType(argTypes, results); }; return mlir::function_interface_impl::parseFunctionOp( - parser, result, /*allowVariadic=*/false, buildFuncType); + parser, result, /*allowVariadic=*/false, + getFunctionTypeAttrName(result.name), buildFuncType, + getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name)); } void FuncOp::print(mlir::OpAsmPrinter &p) { // Dispatch to the FunctionOpInterface provided utility method that prints the // function operation. - mlir::function_interface_impl::printFunctionOp(p, *this, - /*isVariadic=*/false); + mlir::function_interface_impl::printFunctionOp( + p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(), + getArgAttrsAttrName(), getResAttrsAttrName()); } //===----------------------------------------------------------------------===// diff --git a/mlir/example/Ch2/mlir/MLIRGen.cpp b/mlir/example/Ch2/mlir/MLIRGen.cpp index 8941a12..589bd3a 100644 --- a/mlir/example/Ch2/mlir/MLIRGen.cpp +++ b/mlir/example/Ch2/mlir/MLIRGen.cpp @@ -34,7 +34,6 @@ using llvm::ArrayRef; using llvm::cast; using llvm::dyn_cast; using llvm::isa; -using llvm::makeArrayRef; using llvm::ScopedHashTableScope; using llvm::SmallVector; using llvm::StringRef; @@ -109,7 +108,7 @@ class MLIRGenImpl { // Arguments type are uniformly unranked tensors. llvm::SmallVector argTypes(proto.getArgs().size(), getType(VarType{})); - auto funcType = builder.getFunctionType(argTypes, llvm::None); + auto funcType = builder.getFunctionType(argTypes, std::nullopt); return builder.create(location, proto.getName(), funcType); } @@ -214,12 +213,12 @@ class MLIRGenImpl { // 'return' takes an optional expression, handle that case here. mlir::Value expr = nullptr; if (ret.getExpr().has_value()) { - if (!(expr = mlirGen(*ret.getExpr().value()))) return mlir::failure(); + if (!(expr = mlirGen(**ret.getExpr()))) return mlir::failure(); } // Otherwise, this return operation has zero operands. - builder.create( - location, expr ? makeArrayRef(expr) : ArrayRef()); + builder.create(location, + expr ? ArrayRef(expr) : ArrayRef()); return mlir::success(); } @@ -259,7 +258,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. auto dataAttribute = - mlir::DenseElementsAttr::get(dataType, llvm::makeArrayRef(data)); + mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(data)); // Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build` // method. diff --git a/mlir/example/Ch3/include/toy/AST.h b/mlir/example/Ch3/include/toy/AST.h index e41370b..c9d1bdb 100644 --- a/mlir/example/Ch3/include/toy/AST.h +++ b/mlir/example/Ch3/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -131,15 +132,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch3/include/toy/Ops.td b/mlir/example/Ch3/include/toy/Ops.td index 1646bb8..41536c0 100644 --- a/mlir/example/Ch3/include/toy/Ops.td +++ b/mlir/example/Ch3/include/toy/Ops.td @@ -22,7 +22,7 @@ include "mlir/Interfaces/SideEffectInterfaces.td" def Toy_Dialect : Dialect { let name = "toy"; let cppNamespace = "::mlir::toy"; - let emitAccessorPrefix = kEmitAccessorPrefix_Prefixed; + let useFoldAPI = kEmitFoldAdaptorFolder; } // Base class for toy dialect operations. This operation inherits from the base @@ -43,9 +43,9 @@ class Toy_Op traits = []> : // We define a toy operation by inheriting from our base 'Toy_Op' class above. // Here we provide the mnemonic and a list of traits for the operation. The -// constant operation is marked as 'NoSideEffect' as it is a pure operation +// constant operation is marked as 'Pure' as it is a pure operation // and may be removed if dead. -def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { +def ConstantOp : Toy_Op<"constant", [Pure]> { // Provide a summary and description for this operation. This can be used to // auto-generate documentation of the operations within our dialect. let summary = "constant"; @@ -89,7 +89,7 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { // AddOp //===----------------------------------------------------------------------===// -def AddOp : Toy_Op<"add", [NoSideEffect]> { +def AddOp : Toy_Op<"add", [Pure]> { let summary = "element-wise addition operation"; let description = [{ The "add" operation performs element-wise addition between two tensors. @@ -113,7 +113,7 @@ def AddOp : Toy_Op<"add", [NoSideEffect]> { //===----------------------------------------------------------------------===// def FuncOp : Toy_Op<"func", [ - FunctionOpInterface, IsolatedFromAbove, Symbol + FunctionOpInterface, IsolatedFromAbove ]> { let summary = "user defined function operation"; let description = [{ @@ -134,7 +134,9 @@ def FuncOp : Toy_Op<"func", [ let arguments = (ins SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type + TypeAttrOf:$function_type, + OptionalAttr:$arg_attrs, + OptionalAttr:$res_attrs ); let regions = (region AnyRegion:$body); @@ -200,7 +202,7 @@ def GenericCallOp : Toy_Op<"generic_call"> { // MulOp //===----------------------------------------------------------------------===// -def MulOp : Toy_Op<"mul", [NoSideEffect]> { +def MulOp : Toy_Op<"mul", [Pure]> { let summary = "element-wise multiplication operation"; let description = [{ The "mul" operation performs element-wise multiplication between two @@ -240,7 +242,7 @@ def PrintOp : Toy_Op<"print"> { // ReshapeOp //===----------------------------------------------------------------------===// -def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { +def ReshapeOp : Toy_Op<"reshape", [Pure]> { let summary = "tensor reshape operation"; let description = [{ Reshape operation is transforming its input tensor into a new tensor with @@ -268,7 +270,7 @@ def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { // ReturnOp //===----------------------------------------------------------------------===// -def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, +def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let summary = "return operation"; let description = [{ @@ -294,7 +296,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // Allow building a ReturnOp with no return operand. let builders = [ - OpBuilder<(ins), [{ build($_builder, $_state, llvm::None); }]> + OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]> ]; // Provide extra utility definitions on the c++ operation class definition. @@ -310,7 +312,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // TransposeOp //===----------------------------------------------------------------------===// -def TransposeOp : Toy_Op<"transpose", [NoSideEffect]> { +def TransposeOp : Toy_Op<"transpose", [Pure]> { let summary = "transpose operation"; let arguments = (ins F64Tensor:$input); diff --git a/mlir/example/Ch3/include/toy/Parser.h b/mlir/example/Ch3/include/toy/Parser.h index 9b19077..ededa4c 100644 --- a/mlir/example/Ch3/include/toy/Parser.h +++ b/mlir/example/Ch3/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -64,7 +64,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch3/mlir/Dialect.cpp b/mlir/example/Ch3/mlir/Dialect.cpp index d842cb3..8953c1b 100644 --- a/mlir/example/Ch3/mlir/Dialect.cpp +++ b/mlir/example/Ch3/mlir/Dialect.cpp @@ -198,14 +198,17 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, std::string &) { return builder.getFunctionType(argTypes, results); }; return mlir::function_interface_impl::parseFunctionOp( - parser, result, /*allowVariadic=*/false, buildFuncType); + parser, result, /*allowVariadic=*/false, + getFunctionTypeAttrName(result.name), buildFuncType, + getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name)); } void FuncOp::print(mlir::OpAsmPrinter &p) { // Dispatch to the FunctionOpInterface provided utility method that prints the // function operation. - mlir::function_interface_impl::printFunctionOp(p, *this, - /*isVariadic=*/false); + mlir::function_interface_impl::printFunctionOp( + p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(), + getArgAttrsAttrName(), getResAttrsAttrName()); } //===----------------------------------------------------------------------===// diff --git a/mlir/example/Ch3/mlir/MLIRGen.cpp b/mlir/example/Ch3/mlir/MLIRGen.cpp index 8941a12..589bd3a 100644 --- a/mlir/example/Ch3/mlir/MLIRGen.cpp +++ b/mlir/example/Ch3/mlir/MLIRGen.cpp @@ -34,7 +34,6 @@ using llvm::ArrayRef; using llvm::cast; using llvm::dyn_cast; using llvm::isa; -using llvm::makeArrayRef; using llvm::ScopedHashTableScope; using llvm::SmallVector; using llvm::StringRef; @@ -109,7 +108,7 @@ class MLIRGenImpl { // Arguments type are uniformly unranked tensors. llvm::SmallVector argTypes(proto.getArgs().size(), getType(VarType{})); - auto funcType = builder.getFunctionType(argTypes, llvm::None); + auto funcType = builder.getFunctionType(argTypes, std::nullopt); return builder.create(location, proto.getName(), funcType); } @@ -214,12 +213,12 @@ class MLIRGenImpl { // 'return' takes an optional expression, handle that case here. mlir::Value expr = nullptr; if (ret.getExpr().has_value()) { - if (!(expr = mlirGen(*ret.getExpr().value()))) return mlir::failure(); + if (!(expr = mlirGen(**ret.getExpr()))) return mlir::failure(); } // Otherwise, this return operation has zero operands. - builder.create( - location, expr ? makeArrayRef(expr) : ArrayRef()); + builder.create(location, + expr ? ArrayRef(expr) : ArrayRef()); return mlir::success(); } @@ -259,7 +258,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. auto dataAttribute = - mlir::DenseElementsAttr::get(dataType, llvm::makeArrayRef(data)); + mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(data)); // Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build` // method. diff --git a/mlir/example/Ch4/include/toy/AST.h b/mlir/example/Ch4/include/toy/AST.h index e41370b..c9d1bdb 100644 --- a/mlir/example/Ch4/include/toy/AST.h +++ b/mlir/example/Ch4/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -131,15 +132,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch4/include/toy/Ops.td b/mlir/example/Ch4/include/toy/Ops.td index 672876e..a91786f 100644 --- a/mlir/example/Ch4/include/toy/Ops.td +++ b/mlir/example/Ch4/include/toy/Ops.td @@ -25,7 +25,7 @@ include "toy/ShapeInferenceInterface.td" def Toy_Dialect : Dialect { let name = "toy"; let cppNamespace = "::mlir::toy"; - let emitAccessorPrefix = kEmitAccessorPrefix_Prefixed; + let useFoldAPI = kEmitFoldAdaptorFolder; } // Base class for toy dialect operations. This operation inherits from the base @@ -46,9 +46,9 @@ class Toy_Op traits = []> : // We define a toy operation by inheriting from our base 'Toy_Op' class above. // Here we provide the mnemonic and a list of traits for the operation. The -// constant operation is marked as 'NoSideEffect' as it is a pure operation +// constant operation is marked as 'Pure' as it is a pure operation // and may be removed if dead. -def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { +def ConstantOp : Toy_Op<"constant", [Pure]> { // Provide a summary and description for this operation. This can be used to // auto-generate documentation of the operations within our dialect. let summary = "constant"; @@ -93,7 +93,7 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { //===----------------------------------------------------------------------===// def AddOp : Toy_Op<"add", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise addition operation"; let description = [{ The "add" operation performs element-wise addition between two tensors. @@ -119,7 +119,7 @@ def AddOp : Toy_Op<"add", def CastOp : Toy_Op<"cast", [ DeclareOpInterfaceMethods, DeclareOpInterfaceMethods, - NoSideEffect, + Pure, SameOperandsAndResultShape ]> { let summary = "shape cast operation"; @@ -143,7 +143,7 @@ def CastOp : Toy_Op<"cast", [ def FuncOp : Toy_Op<"func", [ DeclareOpInterfaceMethods, FunctionOpInterface, - IsolatedFromAbove, Symbol + IsolatedFromAbove ]> { let summary = "user defined function operation"; let description = [{ @@ -164,7 +164,9 @@ def FuncOp : Toy_Op<"func", [ let arguments = (ins SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type + TypeAttrOf:$function_type, + OptionalAttr:$arg_attrs, + OptionalAttr:$res_attrs ); let regions = (region AnyRegion:$body); @@ -232,7 +234,7 @@ def GenericCallOp : Toy_Op<"generic_call", //===----------------------------------------------------------------------===// def MulOp : Toy_Op<"mul", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise multiplication operation"; let description = [{ The "mul" operation performs element-wise multiplication between two @@ -272,7 +274,7 @@ def PrintOp : Toy_Op<"print"> { // ReshapeOp //===----------------------------------------------------------------------===// -def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { +def ReshapeOp : Toy_Op<"reshape", [Pure]> { let summary = "tensor reshape operation"; let description = [{ Reshape operation is transforming its input tensor into a new tensor with @@ -300,7 +302,7 @@ def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { // ReturnOp //===----------------------------------------------------------------------===// -def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, +def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let summary = "return operation"; let description = [{ @@ -326,7 +328,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // Allow building a ReturnOp with no return operand. let builders = [ - OpBuilder<(ins), [{ build($_builder, $_state, llvm::None); }]> + OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]> ]; // Provide extra utility definitions on the c++ operation class definition. @@ -343,7 +345,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, //===----------------------------------------------------------------------===// def TransposeOp : Toy_Op<"transpose", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "transpose operation"; let arguments = (ins F64Tensor:$input); diff --git a/mlir/example/Ch4/include/toy/Parser.h b/mlir/example/Ch4/include/toy/Parser.h index 9b19077..ededa4c 100644 --- a/mlir/example/Ch4/include/toy/Parser.h +++ b/mlir/example/Ch4/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -64,7 +64,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch4/mlir/Dialect.cpp b/mlir/example/Ch4/mlir/Dialect.cpp index 3440fd8..594557e 100644 --- a/mlir/example/Ch4/mlir/Dialect.cpp +++ b/mlir/example/Ch4/mlir/Dialect.cpp @@ -44,14 +44,12 @@ struct ToyInlinerInterface : public DialectInlinerInterface { } /// All operations within toy can be inlined. - bool isLegalToInline(Operation *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { return true; } // All functions within toy can be inlined. - bool isLegalToInline(Region *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Region *, Region *, bool, IRMapping &) const final { return true; } @@ -286,14 +284,17 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, std::string &) { return builder.getFunctionType(argTypes, results); }; return mlir::function_interface_impl::parseFunctionOp( - parser, result, /*allowVariadic=*/false, buildFuncType); + parser, result, /*allowVariadic=*/false, + getFunctionTypeAttrName(result.name), buildFuncType, + getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name)); } void FuncOp::print(mlir::OpAsmPrinter &p) { // Dispatch to the FunctionOpInterface provided utility method that prints the // function operation. - mlir::function_interface_impl::printFunctionOp(p, *this, - /*isVariadic=*/false); + mlir::function_interface_impl::printFunctionOp( + p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(), + getArgAttrsAttrName(), getResAttrsAttrName()); } /// Returns the region on the function operation that is callable. diff --git a/mlir/example/Ch4/mlir/MLIRGen.cpp b/mlir/example/Ch4/mlir/MLIRGen.cpp index ed598fd..58ec129 100644 --- a/mlir/example/Ch4/mlir/MLIRGen.cpp +++ b/mlir/example/Ch4/mlir/MLIRGen.cpp @@ -34,7 +34,6 @@ using llvm::ArrayRef; using llvm::cast; using llvm::dyn_cast; using llvm::isa; -using llvm::makeArrayRef; using llvm::ScopedHashTableScope; using llvm::SmallVector; using llvm::StringRef; @@ -109,7 +108,7 @@ class MLIRGenImpl { // Arguments type are uniformly unranked tensors. llvm::SmallVector argTypes(proto.getArgs().size(), getType(VarType{})); - auto funcType = builder.getFunctionType(argTypes, llvm::None); + auto funcType = builder.getFunctionType(argTypes, std::nullopt); return builder.create(location, proto.getName(), funcType); } @@ -217,12 +216,12 @@ class MLIRGenImpl { // 'return' takes an optional expression, handle that case here. mlir::Value expr = nullptr; if (ret.getExpr().has_value()) { - if (!(expr = mlirGen(*ret.getExpr().value()))) return mlir::failure(); + if (!(expr = mlirGen(**ret.getExpr()))) return mlir::failure(); } // Otherwise, this return operation has zero operands. - builder.create( - location, expr ? makeArrayRef(expr) : ArrayRef()); + builder.create(location, + expr ? ArrayRef(expr) : ArrayRef()); return mlir::success(); } @@ -262,7 +261,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. auto dataAttribute = - mlir::DenseElementsAttr::get(dataType, llvm::makeArrayRef(data)); + mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(data)); // Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build` // method. diff --git a/mlir/example/Ch5/include/toy/AST.h b/mlir/example/Ch5/include/toy/AST.h index e41370b..c9d1bdb 100644 --- a/mlir/example/Ch5/include/toy/AST.h +++ b/mlir/example/Ch5/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -131,15 +132,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch5/include/toy/Ops.td b/mlir/example/Ch5/include/toy/Ops.td index 714ddef..5cf677d 100644 --- a/mlir/example/Ch5/include/toy/Ops.td +++ b/mlir/example/Ch5/include/toy/Ops.td @@ -25,7 +25,7 @@ include "toy/ShapeInferenceInterface.td" def Toy_Dialect : Dialect { let name = "toy"; let cppNamespace = "::mlir::toy"; - let emitAccessorPrefix = kEmitAccessorPrefix_Prefixed; + let useFoldAPI = kEmitFoldAdaptorFolder; } // Base class for toy dialect operations. This operation inherits from the base @@ -46,9 +46,9 @@ class Toy_Op traits = []> : // We define a toy operation by inheriting from our base 'Toy_Op' class above. // Here we provide the mnemonic and a list of traits for the operation. The -// constant operation is marked as 'NoSideEffect' as it is a pure operation +// constant operation is marked as 'Pure' as it is a pure operation // and may be removed if dead. -def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { +def ConstantOp : Toy_Op<"constant", [Pure]> { // Provide a summary and description for this operation. This can be used to // auto-generate documentation of the operations within our dialect. let summary = "constant"; @@ -93,7 +93,7 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { //===----------------------------------------------------------------------===// def AddOp : Toy_Op<"add", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise addition operation"; let description = [{ The "add" operation performs element-wise addition between two tensors. @@ -119,7 +119,7 @@ def AddOp : Toy_Op<"add", def CastOp : Toy_Op<"cast", [ DeclareOpInterfaceMethods, DeclareOpInterfaceMethods, - NoSideEffect, + Pure, SameOperandsAndResultShape ]> { let summary = "shape cast operation"; @@ -143,7 +143,7 @@ def CastOp : Toy_Op<"cast", [ def FuncOp : Toy_Op<"func", [ DeclareOpInterfaceMethods, FunctionOpInterface, - IsolatedFromAbove, Symbol + IsolatedFromAbove ]> { let summary = "user defined function operation"; let description = [{ @@ -164,7 +164,9 @@ def FuncOp : Toy_Op<"func", [ let arguments = (ins SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type + TypeAttrOf:$function_type, + OptionalAttr:$arg_attrs, + OptionalAttr:$res_attrs ); let regions = (region AnyRegion:$body); @@ -232,7 +234,7 @@ def GenericCallOp : Toy_Op<"generic_call", //===----------------------------------------------------------------------===// def MulOp : Toy_Op<"mul", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise multiplication operation"; let description = [{ The "mul" operation performs element-wise multiplication between two @@ -273,7 +275,7 @@ def PrintOp : Toy_Op<"print"> { // ReshapeOp //===----------------------------------------------------------------------===// -def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { +def ReshapeOp : Toy_Op<"reshape", [Pure]> { let summary = "tensor reshape operation"; let description = [{ Reshape operation is transforming its input tensor into a new tensor with @@ -301,7 +303,7 @@ def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { // ReturnOp //===----------------------------------------------------------------------===// -def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, +def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let summary = "return operation"; let description = [{ @@ -327,7 +329,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // Allow building a ReturnOp with no return operand. let builders = [ - OpBuilder<(ins), [{ build($_builder, $_state, llvm::None); }]> + OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]> ]; // Provide extra utility definitions on the c++ operation class definition. @@ -344,7 +346,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, //===----------------------------------------------------------------------===// def TransposeOp : Toy_Op<"transpose", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "transpose operation"; let arguments = (ins F64Tensor:$input); diff --git a/mlir/example/Ch5/include/toy/Parser.h b/mlir/example/Ch5/include/toy/Parser.h index 9b19077..ededa4c 100644 --- a/mlir/example/Ch5/include/toy/Parser.h +++ b/mlir/example/Ch5/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -64,7 +64,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch5/mlir/Dialect.cpp b/mlir/example/Ch5/mlir/Dialect.cpp index 4371b99..d49228b 100644 --- a/mlir/example/Ch5/mlir/Dialect.cpp +++ b/mlir/example/Ch5/mlir/Dialect.cpp @@ -44,14 +44,12 @@ struct ToyInlinerInterface : public DialectInlinerInterface { } /// All operations within toy can be inlined. - bool isLegalToInline(Operation *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { return true; } // All functions within toy can be inlined. - bool isLegalToInline(Region *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Region *, Region *, bool, IRMapping &) const final { return true; } @@ -286,14 +284,17 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, std::string &) { return builder.getFunctionType(argTypes, results); }; return mlir::function_interface_impl::parseFunctionOp( - parser, result, /*allowVariadic=*/false, buildFuncType); + parser, result, /*allowVariadic=*/false, + getFunctionTypeAttrName(result.name), buildFuncType, + getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name)); } void FuncOp::print(mlir::OpAsmPrinter &p) { // Dispatch to the FunctionOpInterface provided utility method that prints the // function operation. - mlir::function_interface_impl::printFunctionOp(p, *this, - /*isVariadic=*/false); + mlir::function_interface_impl::printFunctionOp( + p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(), + getArgAttrsAttrName(), getResAttrsAttrName()); } /// Returns the region on the function operation that is callable. diff --git a/mlir/example/Ch5/mlir/LowerToAffineLoops.cpp b/mlir/example/Ch5/mlir/LowerToAffineLoops.cpp index 5cb33ef..ab8e7e1 100644 --- a/mlir/example/Ch5/mlir/LowerToAffineLoops.cpp +++ b/mlir/example/Ch5/mlir/LowerToAffineLoops.cpp @@ -14,7 +14,7 @@ #include "llvm/ADT/Sequence.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" +#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/IR/BuiltinDialect.h" @@ -176,7 +176,7 @@ struct ConstantOpLowering : public OpRewritePattern { if (dimension == valueShape.size()) { rewriter.create( loc, rewriter.create(loc, *valueIt++), alloc, - llvm::makeArrayRef(indices)); + llvm::ArrayRef(indices)); return; } @@ -324,10 +324,9 @@ void ToyToAffineLoweringPass::runOnOperation() { // We define the specific operations, or dialects, that are legal targets for // this lowering. In our case, we are lowering to a combination of the - // `Affine`, `Arithmetic`, `Func`, and `MemRef` dialects. - target - .addLegalDialect(); + // `Affine`, `Arith`, `Func`, and `MemRef` dialects. + target.addLegalDialect(); // We also define the Toy dialect as Illegal so that the conversion will fail // if any of these operations are *not* converted. Given that we actually want diff --git a/mlir/example/Ch5/mlir/MLIRGen.cpp b/mlir/example/Ch5/mlir/MLIRGen.cpp index ed598fd..58ec129 100644 --- a/mlir/example/Ch5/mlir/MLIRGen.cpp +++ b/mlir/example/Ch5/mlir/MLIRGen.cpp @@ -34,7 +34,6 @@ using llvm::ArrayRef; using llvm::cast; using llvm::dyn_cast; using llvm::isa; -using llvm::makeArrayRef; using llvm::ScopedHashTableScope; using llvm::SmallVector; using llvm::StringRef; @@ -109,7 +108,7 @@ class MLIRGenImpl { // Arguments type are uniformly unranked tensors. llvm::SmallVector argTypes(proto.getArgs().size(), getType(VarType{})); - auto funcType = builder.getFunctionType(argTypes, llvm::None); + auto funcType = builder.getFunctionType(argTypes, std::nullopt); return builder.create(location, proto.getName(), funcType); } @@ -217,12 +216,12 @@ class MLIRGenImpl { // 'return' takes an optional expression, handle that case here. mlir::Value expr = nullptr; if (ret.getExpr().has_value()) { - if (!(expr = mlirGen(*ret.getExpr().value()))) return mlir::failure(); + if (!(expr = mlirGen(**ret.getExpr()))) return mlir::failure(); } // Otherwise, this return operation has zero operands. - builder.create( - location, expr ? makeArrayRef(expr) : ArrayRef()); + builder.create(location, + expr ? ArrayRef(expr) : ArrayRef()); return mlir::success(); } @@ -262,7 +261,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. auto dataAttribute = - mlir::DenseElementsAttr::get(dataType, llvm::makeArrayRef(data)); + mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(data)); // Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build` // method. diff --git a/mlir/example/Ch6/include/toy/AST.h b/mlir/example/Ch6/include/toy/AST.h index e41370b..c9d1bdb 100644 --- a/mlir/example/Ch6/include/toy/AST.h +++ b/mlir/example/Ch6/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -131,15 +132,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch6/include/toy/Ops.td b/mlir/example/Ch6/include/toy/Ops.td index 2e1945e..e15bcb9 100644 --- a/mlir/example/Ch6/include/toy/Ops.td +++ b/mlir/example/Ch6/include/toy/Ops.td @@ -25,7 +25,7 @@ include "toy/ShapeInferenceInterface.td" def Toy_Dialect : Dialect { let name = "toy"; let cppNamespace = "::mlir::toy"; - let emitAccessorPrefix = kEmitAccessorPrefix_Prefixed; + let useFoldAPI = kEmitFoldAdaptorFolder; } // Base class for toy dialect operations. This operation inherits from the base @@ -46,9 +46,9 @@ class Toy_Op traits = []> : // We define a toy operation by inheriting from our base 'Toy_Op' class above. // Here we provide the mnemonic and a list of traits for the operation. The -// constant operation is marked as 'NoSideEffect' as it is a pure operation +// constant operation is marked as 'Pure' as it is a pure operation // and may be removed if dead. -def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { +def ConstantOp : Toy_Op<"constant", [Pure]> { // Provide a summary and description for this operation. This can be used to // auto-generate documentation of the operations within our dialect. let summary = "constant"; @@ -93,7 +93,7 @@ def ConstantOp : Toy_Op<"constant", [NoSideEffect]> { //===----------------------------------------------------------------------===// def AddOp : Toy_Op<"add", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise addition operation"; let description = [{ The "add" operation performs element-wise addition between two tensors. @@ -119,7 +119,7 @@ def AddOp : Toy_Op<"add", def CastOp : Toy_Op<"cast", [ DeclareOpInterfaceMethods, DeclareOpInterfaceMethods, - NoSideEffect, + Pure, SameOperandsAndResultShape ]> { let summary = "shape cast operation"; @@ -143,7 +143,7 @@ def CastOp : Toy_Op<"cast", [ def FuncOp : Toy_Op<"func", [ DeclareOpInterfaceMethods, FunctionOpInterface, - IsolatedFromAbove, Symbol + IsolatedFromAbove ]> { let summary = "user defined function operation"; let description = [{ @@ -164,7 +164,9 @@ def FuncOp : Toy_Op<"func", [ let arguments = (ins SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type + TypeAttrOf:$function_type, + OptionalAttr:$arg_attrs, + OptionalAttr:$res_attrs ); let regions = (region AnyRegion:$body); @@ -232,7 +234,7 @@ def GenericCallOp : Toy_Op<"generic_call", //===----------------------------------------------------------------------===// def MulOp : Toy_Op<"mul", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise multiplication operation"; let description = [{ The "mul" operation performs element-wise multiplication between two @@ -273,7 +275,7 @@ def PrintOp : Toy_Op<"print"> { // ReshapeOp //===----------------------------------------------------------------------===// -def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { +def ReshapeOp : Toy_Op<"reshape", [Pure]> { let summary = "tensor reshape operation"; let description = [{ Reshape operation is transforming its input tensor into a new tensor with @@ -301,7 +303,7 @@ def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { // ReturnOp //===----------------------------------------------------------------------===// -def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, +def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let summary = "return operation"; let description = [{ @@ -327,7 +329,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // Allow building a ReturnOp with no return operand. let builders = [ - OpBuilder<(ins), [{ build($_builder, $_state, llvm::None); }]> + OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]> ]; // Provide extra utility definitions on the c++ operation class definition. @@ -344,7 +346,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, //===----------------------------------------------------------------------===// def TransposeOp : Toy_Op<"transpose", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "transpose operation"; let arguments = (ins F64Tensor:$input); diff --git a/mlir/example/Ch6/include/toy/Parser.h b/mlir/example/Ch6/include/toy/Parser.h index 9b19077..ededa4c 100644 --- a/mlir/example/Ch6/include/toy/Parser.h +++ b/mlir/example/Ch6/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -64,7 +64,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch6/mlir/Dialect.cpp b/mlir/example/Ch6/mlir/Dialect.cpp index 4371b99..d49228b 100644 --- a/mlir/example/Ch6/mlir/Dialect.cpp +++ b/mlir/example/Ch6/mlir/Dialect.cpp @@ -44,14 +44,12 @@ struct ToyInlinerInterface : public DialectInlinerInterface { } /// All operations within toy can be inlined. - bool isLegalToInline(Operation *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { return true; } // All functions within toy can be inlined. - bool isLegalToInline(Region *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Region *, Region *, bool, IRMapping &) const final { return true; } @@ -286,14 +284,17 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, std::string &) { return builder.getFunctionType(argTypes, results); }; return mlir::function_interface_impl::parseFunctionOp( - parser, result, /*allowVariadic=*/false, buildFuncType); + parser, result, /*allowVariadic=*/false, + getFunctionTypeAttrName(result.name), buildFuncType, + getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name)); } void FuncOp::print(mlir::OpAsmPrinter &p) { // Dispatch to the FunctionOpInterface provided utility method that prints the // function operation. - mlir::function_interface_impl::printFunctionOp(p, *this, - /*isVariadic=*/false); + mlir::function_interface_impl::printFunctionOp( + p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(), + getArgAttrsAttrName(), getResAttrsAttrName()); } /// Returns the region on the function operation that is callable. diff --git a/mlir/example/Ch6/mlir/LowerToAffineLoops.cpp b/mlir/example/Ch6/mlir/LowerToAffineLoops.cpp index 5cb33ef..ab8e7e1 100644 --- a/mlir/example/Ch6/mlir/LowerToAffineLoops.cpp +++ b/mlir/example/Ch6/mlir/LowerToAffineLoops.cpp @@ -14,7 +14,7 @@ #include "llvm/ADT/Sequence.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" +#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/IR/BuiltinDialect.h" @@ -176,7 +176,7 @@ struct ConstantOpLowering : public OpRewritePattern { if (dimension == valueShape.size()) { rewriter.create( loc, rewriter.create(loc, *valueIt++), alloc, - llvm::makeArrayRef(indices)); + llvm::ArrayRef(indices)); return; } @@ -324,10 +324,9 @@ void ToyToAffineLoweringPass::runOnOperation() { // We define the specific operations, or dialects, that are legal targets for // this lowering. In our case, we are lowering to a combination of the - // `Affine`, `Arithmetic`, `Func`, and `MemRef` dialects. - target - .addLegalDialect(); + // `Affine`, `Arith`, `Func`, and `MemRef` dialects. + target.addLegalDialect(); // We also define the Toy dialect as Illegal so that the conversion will fail // if any of these operations are *not* converted. Given that we actually want diff --git a/mlir/example/Ch6/mlir/LowerToLLVM.cpp b/mlir/example/Ch6/mlir/LowerToLLVM.cpp index ee98cd3..d03df2a 100644 --- a/mlir/example/Ch6/mlir/LowerToLLVM.cpp +++ b/mlir/example/Ch6/mlir/LowerToLLVM.cpp @@ -24,7 +24,7 @@ #include "llvm/ADT/Sequence.h" #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" -#include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h" +#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h" @@ -33,7 +33,7 @@ #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" +#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" @@ -152,9 +152,8 @@ class PrintOpLowering : public ConversionPattern { // Get the pointer to the first character in the global string. Value globalPtr = builder.create(loc, global); - Value cst0 = builder.create( - loc, IntegerType::get(builder.getContext(), 64), - builder.getIntegerAttr(builder.getIndexType(), 0)); + Value cst0 = builder.create(loc, builder.getI64Type(), + builder.getIndexAttr(0)); return builder.create( loc, LLVM::LLVMPointerType::get(IntegerType::get(builder.getContext(), 8)), @@ -204,8 +203,7 @@ void ToyToLLVMLoweringPass::runOnOperation() { RewritePatternSet patterns(&getContext()); populateAffineToStdConversionPatterns(patterns); populateSCFToControlFlowConversionPatterns(patterns); - mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter, - patterns); + mlir::arith::populateArithToLLVMConversionPatterns(typeConverter, patterns); populateMemRefToLLVMConversionPatterns(typeConverter, patterns); cf::populateControlFlowToLLVMConversionPatterns(typeConverter, patterns); populateFuncToLLVMConversionPatterns(typeConverter, patterns); diff --git a/mlir/example/Ch6/mlir/MLIRGen.cpp b/mlir/example/Ch6/mlir/MLIRGen.cpp index ed598fd..58ec129 100644 --- a/mlir/example/Ch6/mlir/MLIRGen.cpp +++ b/mlir/example/Ch6/mlir/MLIRGen.cpp @@ -34,7 +34,6 @@ using llvm::ArrayRef; using llvm::cast; using llvm::dyn_cast; using llvm::isa; -using llvm::makeArrayRef; using llvm::ScopedHashTableScope; using llvm::SmallVector; using llvm::StringRef; @@ -109,7 +108,7 @@ class MLIRGenImpl { // Arguments type are uniformly unranked tensors. llvm::SmallVector argTypes(proto.getArgs().size(), getType(VarType{})); - auto funcType = builder.getFunctionType(argTypes, llvm::None); + auto funcType = builder.getFunctionType(argTypes, std::nullopt); return builder.create(location, proto.getName(), funcType); } @@ -217,12 +216,12 @@ class MLIRGenImpl { // 'return' takes an optional expression, handle that case here. mlir::Value expr = nullptr; if (ret.getExpr().has_value()) { - if (!(expr = mlirGen(*ret.getExpr().value()))) return mlir::failure(); + if (!(expr = mlirGen(**ret.getExpr()))) return mlir::failure(); } // Otherwise, this return operation has zero operands. - builder.create( - location, expr ? makeArrayRef(expr) : ArrayRef()); + builder.create(location, + expr ? ArrayRef(expr) : ArrayRef()); return mlir::success(); } @@ -262,7 +261,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. auto dataAttribute = - mlir::DenseElementsAttr::get(dataType, llvm::makeArrayRef(data)); + mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(data)); // Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build` // method. diff --git a/mlir/example/Ch7/include/toy/AST.h b/mlir/example/Ch7/include/toy/AST.h index ef0f47a..faaf6e8 100644 --- a/mlir/example/Ch7/include/toy/AST.h +++ b/mlir/example/Ch7/include/toy/AST.h @@ -15,6 +15,7 @@ #ifndef TOY_AST_H #define TOY_AST_H +#include #include #include @@ -151,15 +152,15 @@ class VarDeclExprAST : public ExprAST { /// Expression class for a return operator. class ReturnExprAST : public ExprAST { - llvm::Optional> expr; + std::optional> expr; public: - ReturnExprAST(Location loc, llvm::Optional> expr) + ReturnExprAST(Location loc, std::optional> expr) : ExprAST(Expr_Return, std::move(loc)), expr(std::move(expr)) {} - llvm::Optional getExpr() { + std::optional getExpr() { if (expr.has_value()) return expr->get(); - return llvm::None; + return std::nullopt; } /// LLVM style RTTI diff --git a/mlir/example/Ch7/include/toy/Ops.td b/mlir/example/Ch7/include/toy/Ops.td index 6b462c5..04d6e31 100644 --- a/mlir/example/Ch7/include/toy/Ops.td +++ b/mlir/example/Ch7/include/toy/Ops.td @@ -25,7 +25,6 @@ include "toy/ShapeInferenceInterface.td" def Toy_Dialect : Dialect { let name = "toy"; let cppNamespace = "::mlir::toy"; - let emitAccessorPrefix = kEmitAccessorPrefix_Prefixed; // We set this bit to generate a declaration of the `materializeConstant` // method so that we can materialize constants for our toy operations. @@ -34,6 +33,8 @@ def Toy_Dialect : Dialect { // We set this bit to generate the declarations for the dialect's type parsing // and printing hooks. let useDefaultTypePrinterParser = 1; + + let useFoldAPI = kEmitFoldAdaptorFolder; } // Base class for toy dialect operations. This operation inherits from the base @@ -64,10 +65,10 @@ def Toy_Type : AnyTypeOf<[F64Tensor, Toy_StructType]>; // We define a toy operation by inheriting from our base 'Toy_Op' class above. // Here we provide the mnemonic and a list of traits for the operation. The -// constant operation is marked as 'NoSideEffect' as it is a pure operation +// constant operation is marked as 'Pure' as it is a pure operation // and may be removed if dead. def ConstantOp : Toy_Op<"constant", - [ConstantLike, NoSideEffect, + [ConstantLike, Pure, DeclareOpInterfaceMethods]> { // Provide a summary and description for this operation. This can be used to // auto-generate documentation of the operations within our dialect. @@ -116,7 +117,7 @@ def ConstantOp : Toy_Op<"constant", //===----------------------------------------------------------------------===// def AddOp : Toy_Op<"add", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise addition operation"; let description = [{ The "add" operation performs element-wise addition between two tensors. @@ -142,7 +143,7 @@ def AddOp : Toy_Op<"add", def CastOp : Toy_Op<"cast", [ DeclareOpInterfaceMethods, DeclareOpInterfaceMethods, - NoSideEffect, + Pure, SameOperandsAndResultShape ]> { let summary = "shape cast operation"; @@ -166,7 +167,7 @@ def CastOp : Toy_Op<"cast", [ def FuncOp : Toy_Op<"func", [ DeclareOpInterfaceMethods, FunctionOpInterface, - IsolatedFromAbove, Symbol + IsolatedFromAbove ]> { let summary = "user defined function operation"; let description = [{ @@ -187,7 +188,9 @@ def FuncOp : Toy_Op<"func", [ let arguments = (ins SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type + TypeAttrOf:$function_type, + OptionalAttr:$arg_attrs, + OptionalAttr:$res_attrs ); let regions = (region AnyRegion:$body); @@ -256,7 +259,7 @@ def GenericCallOp : Toy_Op<"generic_call", //===----------------------------------------------------------------------===// def MulOp : Toy_Op<"mul", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "element-wise multiplication operation"; let description = [{ The "mul" operation performs element-wise multiplication between two @@ -297,7 +300,7 @@ def PrintOp : Toy_Op<"print"> { // ReshapeOp //===----------------------------------------------------------------------===// -def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { +def ReshapeOp : Toy_Op<"reshape", [Pure]> { let summary = "tensor reshape operation"; let description = [{ Reshape operation is transforming its input tensor into a new tensor with @@ -325,7 +328,7 @@ def ReshapeOp : Toy_Op<"reshape", [NoSideEffect]> { // ReturnOp //===----------------------------------------------------------------------===// -def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, +def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">, Terminator]> { let summary = "return operation"; let description = [{ @@ -351,7 +354,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // Allow building a ReturnOp with no return operand. let builders = [ - OpBuilder<(ins), [{ build($_builder, $_state, llvm::None); }]> + OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]> ]; // Provide extra utility definitions on the c++ operation class definition. @@ -367,7 +370,7 @@ def ReturnOp : Toy_Op<"return", [NoSideEffect, HasParent<"FuncOp">, // StructAccessOp //===----------------------------------------------------------------------===// -def StructAccessOp : Toy_Op<"struct_access", [NoSideEffect]> { +def StructAccessOp : Toy_Op<"struct_access", [Pure]> { let summary = "struct access"; let description = [{ Access the Nth element of a value returning a struct type. @@ -396,7 +399,7 @@ def StructAccessOp : Toy_Op<"struct_access", [NoSideEffect]> { // StructConstantOp //===----------------------------------------------------------------------===// -def StructConstantOp : Toy_Op<"struct_constant", [ConstantLike, NoSideEffect]> { +def StructConstantOp : Toy_Op<"struct_constant", [ConstantLike, Pure]> { let summary = "struct constant"; let description = [{ Constant operation turns a literal struct value into an SSA value. The data @@ -425,7 +428,7 @@ def StructConstantOp : Toy_Op<"struct_constant", [ConstantLike, NoSideEffect]> { //===----------------------------------------------------------------------===// def TransposeOp : Toy_Op<"transpose", - [NoSideEffect, DeclareOpInterfaceMethods]> { + [Pure, DeclareOpInterfaceMethods]> { let summary = "transpose operation"; let arguments = (ins F64Tensor:$input); diff --git a/mlir/example/Ch7/include/toy/Parser.h b/mlir/example/Ch7/include/toy/Parser.h index 77fb9a6..a5ddacd 100644 --- a/mlir/example/Ch7/include/toy/Parser.h +++ b/mlir/example/Ch7/include/toy/Parser.h @@ -15,10 +15,10 @@ #define TOY_PARSER_H #include +#include #include #include -#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/raw_ostream.h" @@ -79,7 +79,7 @@ class Parser { lexer.consume(tok_return); // return takes an optional argument - llvm::Optional> expr; + std::optional> expr; if (lexer.getCurToken() != ';') { expr = parseExpression(); if (!expr) return nullptr; diff --git a/mlir/example/Ch7/mlir/Dialect.cpp b/mlir/example/Ch7/mlir/Dialect.cpp index d5a9d61..9053cbd 100644 --- a/mlir/example/Ch7/mlir/Dialect.cpp +++ b/mlir/example/Ch7/mlir/Dialect.cpp @@ -45,14 +45,12 @@ struct ToyInlinerInterface : public DialectInlinerInterface { } /// All operations within toy can be inlined. - bool isLegalToInline(Operation *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { return true; } // All functions within toy can be inlined. - bool isLegalToInline(Region *, Region *, bool, - BlockAndValueMapping &) const final { + bool isLegalToInline(Region *, Region *, bool, IRMapping &) const final { return true; } @@ -315,14 +313,17 @@ mlir::ParseResult FuncOp::parse(mlir::OpAsmParser &parser, std::string &) { return builder.getFunctionType(argTypes, results); }; return mlir::function_interface_impl::parseFunctionOp( - parser, result, /*allowVariadic=*/false, buildFuncType); + parser, result, /*allowVariadic=*/false, + getFunctionTypeAttrName(result.name), buildFuncType, + getArgAttrsAttrName(result.name), getResAttrsAttrName(result.name)); } void FuncOp::print(mlir::OpAsmPrinter &p) { // Dispatch to the FunctionOpInterface provided utility method that prints the // function operation. - mlir::function_interface_impl::printFunctionOp(p, *this, - /*isVariadic=*/false); + mlir::function_interface_impl::printFunctionOp( + p, *this, /*isVariadic=*/false, getFunctionTypeAttrName(), + getArgAttrsAttrName(), getResAttrsAttrName()); } /// Returns the region on the function operation that is callable. diff --git a/mlir/example/Ch7/mlir/LowerToAffineLoops.cpp b/mlir/example/Ch7/mlir/LowerToAffineLoops.cpp index 5cb33ef..ab8e7e1 100644 --- a/mlir/example/Ch7/mlir/LowerToAffineLoops.cpp +++ b/mlir/example/Ch7/mlir/LowerToAffineLoops.cpp @@ -14,7 +14,7 @@ #include "llvm/ADT/Sequence.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" +#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" #include "mlir/IR/BuiltinDialect.h" @@ -176,7 +176,7 @@ struct ConstantOpLowering : public OpRewritePattern { if (dimension == valueShape.size()) { rewriter.create( loc, rewriter.create(loc, *valueIt++), alloc, - llvm::makeArrayRef(indices)); + llvm::ArrayRef(indices)); return; } @@ -324,10 +324,9 @@ void ToyToAffineLoweringPass::runOnOperation() { // We define the specific operations, or dialects, that are legal targets for // this lowering. In our case, we are lowering to a combination of the - // `Affine`, `Arithmetic`, `Func`, and `MemRef` dialects. - target - .addLegalDialect(); + // `Affine`, `Arith`, `Func`, and `MemRef` dialects. + target.addLegalDialect(); // We also define the Toy dialect as Illegal so that the conversion will fail // if any of these operations are *not* converted. Given that we actually want diff --git a/mlir/example/Ch7/mlir/LowerToLLVM.cpp b/mlir/example/Ch7/mlir/LowerToLLVM.cpp index ee98cd3..d03df2a 100644 --- a/mlir/example/Ch7/mlir/LowerToLLVM.cpp +++ b/mlir/example/Ch7/mlir/LowerToLLVM.cpp @@ -24,7 +24,7 @@ #include "llvm/ADT/Sequence.h" #include "mlir/Conversion/AffineToStandard/AffineToStandard.h" -#include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h" +#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h" #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h" #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h" @@ -33,7 +33,7 @@ #include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h" #include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h" #include "mlir/Dialect/Affine/IR/AffineOps.h" -#include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" +#include "mlir/Dialect/Arith/IR/Arith.h" #include "mlir/Dialect/Func/IR/FuncOps.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" #include "mlir/Dialect/MemRef/IR/MemRef.h" @@ -152,9 +152,8 @@ class PrintOpLowering : public ConversionPattern { // Get the pointer to the first character in the global string. Value globalPtr = builder.create(loc, global); - Value cst0 = builder.create( - loc, IntegerType::get(builder.getContext(), 64), - builder.getIntegerAttr(builder.getIndexType(), 0)); + Value cst0 = builder.create(loc, builder.getI64Type(), + builder.getIndexAttr(0)); return builder.create( loc, LLVM::LLVMPointerType::get(IntegerType::get(builder.getContext(), 8)), @@ -204,8 +203,7 @@ void ToyToLLVMLoweringPass::runOnOperation() { RewritePatternSet patterns(&getContext()); populateAffineToStdConversionPatterns(patterns); populateSCFToControlFlowConversionPatterns(patterns); - mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter, - patterns); + mlir::arith::populateArithToLLVMConversionPatterns(typeConverter, patterns); populateMemRefToLLVMConversionPatterns(typeConverter, patterns); cf::populateControlFlowToLLVMConversionPatterns(typeConverter, patterns); populateFuncToLLVMConversionPatterns(typeConverter, patterns); diff --git a/mlir/example/Ch7/mlir/MLIRGen.cpp b/mlir/example/Ch7/mlir/MLIRGen.cpp index b340cf5..5ba4f18 100644 --- a/mlir/example/Ch7/mlir/MLIRGen.cpp +++ b/mlir/example/Ch7/mlir/MLIRGen.cpp @@ -14,6 +14,7 @@ #include "toy/MLIRGen.h" #include +#include #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/ScopedHashTable.h" @@ -34,7 +35,6 @@ using llvm::ArrayRef; using llvm::cast; using llvm::dyn_cast; using llvm::isa; -using llvm::makeArrayRef; using llvm::ScopedHashTableScope; using llvm::SmallVector; using llvm::StringRef; @@ -162,7 +162,7 @@ class MLIRGenImpl { if (!type) return nullptr; argTypes.push_back(type); } - auto funcType = builder.getFunctionType(argTypes, llvm::None); + auto funcType = builder.getFunctionType(argTypes, std::nullopt); return builder.create(location, proto.getName(), funcType); } @@ -256,22 +256,22 @@ class MLIRGenImpl { } /// Return the numeric member index of the given struct access expression. - llvm::Optional getMemberIndex(BinaryExprAST &accessOp) { + std::optional getMemberIndex(BinaryExprAST &accessOp) { assert(accessOp.getOp() == '.' && "expected access operation"); // Lookup the struct node for the LHS. StructAST *structAST = getStructFor(accessOp.getLHS()); - if (!structAST) return llvm::None; + if (!structAST) return std::nullopt; // Get the name from the RHS. VariableExprAST *name = llvm::dyn_cast(accessOp.getRHS()); - if (!name) return llvm::None; + if (!name) return std::nullopt; auto structVars = structAST->getVariables(); const auto *it = llvm::find_if(structVars, [&](auto &var) { return var->getName() == name->getName(); }); - if (it == structVars.end()) return llvm::None; + if (it == structVars.end()) return std::nullopt; return it - structVars.begin(); } @@ -294,7 +294,7 @@ class MLIRGenImpl { // If this is an access operation, handle it immediately. if (binop.getOp() == '.') { - llvm::Optional accessIndex = getMemberIndex(binop); + std::optional accessIndex = getMemberIndex(binop); if (!accessIndex) { emitError(location, "invalid access into struct expression"); return nullptr; @@ -338,12 +338,12 @@ class MLIRGenImpl { // 'return' takes an optional expression, handle that case here. mlir::Value expr = nullptr; if (ret.getExpr().has_value()) { - if (!(expr = mlirGen(*ret.getExpr().value()))) return mlir::failure(); + if (!(expr = mlirGen(**ret.getExpr()))) return mlir::failure(); } // Otherwise, this return operation has zero operands. - builder.create( - location, expr ? makeArrayRef(expr) : ArrayRef()); + builder.create(location, + expr ? ArrayRef(expr) : ArrayRef()); return mlir::success(); } @@ -380,7 +380,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. - return mlir::DenseElementsAttr::get(dataType, llvm::makeArrayRef(data)); + return mlir::DenseElementsAttr::get(dataType, llvm::ArrayRef(data)); } mlir::DenseElementsAttr getConstantAttr(NumberExprAST &lit) { // The type of this attribute is tensor of 64-bit floating-point with no @@ -391,7 +391,7 @@ class MLIRGenImpl { // This is the actual attribute that holds the list of values for this // tensor literal. return mlir::DenseElementsAttr::get(dataType, - llvm::makeArrayRef(lit.getValue())); + llvm::ArrayRef(lit.getValue())); } /// Emit a constant for a struct literal. It will be emitted as an array of /// other literals in an Attribute attached to a `toy.struct_constant` @@ -405,10 +405,10 @@ class MLIRGenImpl { for (auto &var : lit.getValues()) { if (auto *number = llvm::dyn_cast(var.get())) { attrElements.push_back(getConstantAttr(*number)); - typeElements.push_back(getType(llvm::None)); + typeElements.push_back(getType(std::nullopt)); } else if (auto *lit = llvm::dyn_cast(var.get())) { attrElements.push_back(getConstantAttr(*lit)); - typeElements.push_back(getType(llvm::None)); + typeElements.push_back(getType(std::nullopt)); } else { auto *structLit = llvm::cast(var.get()); auto attrTypePair = getConstantAttr(*structLit); diff --git a/mlir/example/Ch7/mlir/ToyCombine.cpp b/mlir/example/Ch7/mlir/ToyCombine.cpp index d8c3fd7..ec53d0b 100644 --- a/mlir/example/Ch7/mlir/ToyCombine.cpp +++ b/mlir/example/Ch7/mlir/ToyCombine.cpp @@ -25,18 +25,14 @@ namespace { } // namespace /// Fold constants. -OpFoldResult ConstantOp::fold(ArrayRef operands) { - return getValue(); -} +OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); } /// Fold struct constants. -OpFoldResult StructConstantOp::fold(ArrayRef operands) { - return getValue(); -} +OpFoldResult StructConstantOp::fold(FoldAdaptor adaptor) { return getValue(); } /// Fold simple struct access operations that access into a constant. -OpFoldResult StructAccessOp::fold(ArrayRef operands) { - auto structAttr = operands.front().dyn_cast_or_null(); +OpFoldResult StructAccessOp::fold(FoldAdaptor adaptor) { + auto structAttr = adaptor.getInput().dyn_cast_or_null(); if (!structAttr) return nullptr; size_t elementIndex = getIndex(); diff --git a/mlir/example/README.md b/mlir/example/README.md index ed827c6..9a03256 100644 --- a/mlir/example/README.md +++ b/mlir/example/README.md @@ -23,7 +23,7 @@ update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 20 conda create -n mlir -y conda activate mlir # conda install cmake ninja clang-format clang lld ncurses mlir llvm -c conda-forge -conda install cmake ninja clang-format clang=15.* mlir=15.* llvm=15.0.7 -c conda-forge -y +conda install cmake ninja clang-format clang mlir llvm=16.* -c conda-forge -y ``` ## build example