Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion mlir/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ add_subdirectory(Ch4)
add_subdirectory(Ch5)
add_subdirectory(Ch6)
add_subdirectory(Ch7)
add_subdirectory(Ch8)
add_subdirectory(transform_Ch2)
add_subdirectory(transform_Ch3)
add_subdirectory(transform_Ch4)
1 change: 1 addition & 0 deletions mlir/example/Ch1/include/toy/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "llvm/ADT/StringRef.h"

#include <cstdlib>
#include <memory>
#include <string>

Expand Down
2 changes: 1 addition & 1 deletion mlir/example/Ch1/parser/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void ASTDumper::dump(NumberExprAST *num) {
/// [ [ 1, 2 ], [ 3, 4 ] ]
/// We print out such array with the dimensions spelled out at every level:
/// <2,2>[<2>[ 1, 2 ], <2>[ 3, 4 ] ]
void printLitHelper(ExprAST *litOrNum) {
static void printLitHelper(ExprAST *litOrNum) {
// Inside a literal expression we can have either a number or another literal
if (auto *num = llvm::dyn_cast<NumberExprAST>(litOrNum)) {
llvm::errs() << num->getValue();
Expand Down
3 changes: 2 additions & 1 deletion mlir/example/Ch1/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ static cl::opt<enum Action>
cl::values(clEnumValN(DumpAST, "ast", "output the AST dump")));

/// Returns a Toy AST resulting from parsing the file or a nullptr on error.
std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
static std::unique_ptr<toy::ModuleAST>
parseInputFile(llvm::StringRef filename) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(filename);
if (std::error_code ec = fileOrErr.getError()) {
Expand Down
1 change: 1 addition & 0 deletions mlir/example/Ch2/include/toy/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "llvm/ADT/StringRef.h"

#include <cstdlib>
#include <memory>
#include <string>

Expand Down
4 changes: 2 additions & 2 deletions mlir/example/Ch2/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def ConstantOp : Toy_Op<"constant", [Pure]> {

// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
// using `builder.create<ConstantOp>(...)`.
// using `ConstantOp::create(builder, ...)`.
let builders = [
// Build a constant with a given constant tensor value.
OpBuilder<(ins "DenseElementsAttr":$value), [{
Expand Down Expand Up @@ -297,7 +297,7 @@ def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">,

// Allow building a ReturnOp with no return operand.
let builders = [
OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]>
OpBuilder<(ins), [{ build($_builder, $_state, {}); }]>
];

// Provide extra utility definitions on the c++ operation class definition.
Expand Down
33 changes: 16 additions & 17 deletions mlir/example/Ch2/mlir/MLIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class MLIRGenImpl {
// Arguments type are uniformly unranked tensors.
llvm::SmallVector<mlir::Type, 4> argTypes(proto.getArgs().size(),
getType(VarType{}));
auto funcType = builder.getFunctionType(argTypes, std::nullopt);
return builder.create<mlir::toy::FuncOp>(location, proto.getName(),
funcType);
auto funcType = builder.getFunctionType(argTypes, {});
return mlir::toy::FuncOp::create(builder, location, proto.getName(),
funcType);
}

/// Emit a new function and add it to the MLIR module.
Expand Down Expand Up @@ -166,7 +166,7 @@ class MLIRGenImpl {
if (!entryBlock.empty())
returnOp = dyn_cast<ReturnOp>(entryBlock.back());
if (!returnOp) {
builder.create<ReturnOp>(loc(funcAST.getProto()->loc()));
ReturnOp::create(builder, loc(funcAST.getProto()->loc()));
} else if (returnOp.hasOperand()) {
// Otherwise, if this return operation has an operand then add a result to
// the function.
Expand Down Expand Up @@ -202,9 +202,9 @@ class MLIRGenImpl {
// support '+' and '*'.
switch (binop.getOp()) {
case '+':
return builder.create<AddOp>(location, lhs, rhs);
return AddOp::create(builder, location, lhs, rhs);
case '*':
return builder.create<MulOp>(location, lhs, rhs);
return MulOp::create(builder, location, lhs, rhs);
}

emitError(location, "invalid binary operator '") << binop.getOp() << "'";
Expand Down Expand Up @@ -235,8 +235,8 @@ class MLIRGenImpl {
}

// Otherwise, this return operation has zero operands.
builder.create<ReturnOp>(location,
expr ? ArrayRef(expr) : ArrayRef<mlir::Value>());
ReturnOp::create(builder, location,
expr ? ArrayRef(expr) : ArrayRef<mlir::Value>());
return mlir::success();
}

Expand Down Expand Up @@ -264,8 +264,7 @@ class MLIRGenImpl {
// The attribute is a vector with a floating point value per element
// (number) in the array, see `collectData()` below for more details.
std::vector<double> data;
data.reserve(std::accumulate(lit.getDims().begin(), lit.getDims().end(), 1,
std::multiplies<int>()));
data.reserve(llvm::product_of(lit.getDims()));
collectData(lit, data);

// The type of this attribute is tensor of 64-bit floating-point with the
Expand All @@ -280,7 +279,7 @@ class MLIRGenImpl {

// Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build`
// method.
return builder.create<ConstantOp>(loc(lit.loc()), type, dataAttribute);
return ConstantOp::create(builder, loc(lit.loc()), type, dataAttribute);
}

/// Recursive helper function to accumulate the data that compose an array
Expand Down Expand Up @@ -325,13 +324,13 @@ class MLIRGenImpl {
"does not accept multiple arguments");
return nullptr;
}
return builder.create<TransposeOp>(location, operands[0]);
return TransposeOp::create(builder, location, operands[0]);
}

// Otherwise this is a call to a user-defined function. Calls to
// user-defined functions are mapped to a custom call that takes the callee
// name as an attribute.
return builder.create<GenericCallOp>(location, callee, operands);
return GenericCallOp::create(builder, location, callee, operands);
}

/// Emit a print expression. It emits specific operations for two builtins:
Expand All @@ -341,13 +340,13 @@ class MLIRGenImpl {
if (!arg)
return mlir::failure();

builder.create<PrintOp>(loc(call.loc()), arg);
PrintOp::create(builder, loc(call.loc()), arg);
return mlir::success();
}

/// Emit a constant for a single number (FIXME: semantic? broadcast?)
mlir::Value mlirGen(NumberExprAST &num) {
return builder.create<ConstantOp>(loc(num.loc()), num.getValue());
return ConstantOp::create(builder, loc(num.loc()), num.getValue());
}

/// Dispatch codegen for the right expression subclass using RTTI.
Expand Down Expand Up @@ -391,8 +390,8 @@ class MLIRGenImpl {
// with specific shape, we emit a "reshape" operation. It will get
// optimized out later as needed.
if (!vardecl.getType().shape.empty()) {
value = builder.create<ReshapeOp>(loc(vardecl.loc()),
getType(vardecl.getType()), value);
value = ReshapeOp::create(builder, loc(vardecl.loc()),
getType(vardecl.getType()), value);
}

// Register the value in the symbol table.
Expand Down
2 changes: 1 addition & 1 deletion mlir/example/Ch2/parser/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void ASTDumper::dump(NumberExprAST *num) {
/// [ [ 1, 2 ], [ 3, 4 ] ]
/// We print out such array with the dimensions spelled out at every level:
/// <2,2>[<2>[ 1, 2 ], <2>[ 3, 4 ] ]
void printLitHelper(ExprAST *litOrNum) {
static void printLitHelper(ExprAST *litOrNum) {
// Inside a literal expression we can have either a number or another literal
if (auto *num = llvm::dyn_cast<NumberExprAST>(litOrNum)) {
llvm::errs() << num->getValue();
Expand Down
7 changes: 4 additions & 3 deletions mlir/example/Ch2/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ static cl::opt<enum Action> emitAction(
cl::values(clEnumValN(DumpMLIR, "mlir", "output the MLIR dump")));

/// Returns a Toy AST resulting from parsing the file or a nullptr on error.
std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
static std::unique_ptr<toy::ModuleAST>
parseInputFile(llvm::StringRef filename) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(filename);
if (std::error_code ec = fileOrErr.getError()) {
Expand All @@ -71,7 +72,7 @@ std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
return parser.parseModule();
}

int dumpMLIR() {
static int dumpMLIR() {
mlir::MLIRContext context;
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
Expand Down Expand Up @@ -112,7 +113,7 @@ int dumpMLIR() {
return 0;
}

int dumpAST() {
static int dumpAST() {
if (inputType == InputType::MLIR) {
llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
return 5;
Expand Down
1 change: 1 addition & 0 deletions mlir/example/Ch3/include/toy/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "llvm/ADT/StringRef.h"

#include <cstdlib>
#include <memory>
#include <string>

Expand Down
4 changes: 2 additions & 2 deletions mlir/example/Ch3/include/toy/Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def ConstantOp : Toy_Op<"constant", [Pure]> {

// Add custom build methods for the constant operation. These method populates
// the `state` that MLIR uses to create operations, i.e. these are used when
// using `builder.create<ConstantOp>(...)`.
// using `ConstantOp::create(builder, ...)`.
let builders = [
// Build a constant with a given constant tensor value.
OpBuilder<(ins "DenseElementsAttr":$value), [{
Expand Down Expand Up @@ -298,7 +298,7 @@ def ReturnOp : Toy_Op<"return", [Pure, HasParent<"FuncOp">,

// Allow building a ReturnOp with no return operand.
let builders = [
OpBuilder<(ins), [{ build($_builder, $_state, std::nullopt); }]>
OpBuilder<(ins), [{ build($_builder, $_state, {}); }]>
];

// Provide extra utility definitions on the c++ operation class definition.
Expand Down
33 changes: 16 additions & 17 deletions mlir/example/Ch3/mlir/MLIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class MLIRGenImpl {
// Arguments type are uniformly unranked tensors.
llvm::SmallVector<mlir::Type, 4> argTypes(proto.getArgs().size(),
getType(VarType{}));
auto funcType = builder.getFunctionType(argTypes, std::nullopt);
return builder.create<mlir::toy::FuncOp>(location, proto.getName(),
funcType);
auto funcType = builder.getFunctionType(argTypes, /*results=*/{});
return mlir::toy::FuncOp::create(builder, location, proto.getName(),
funcType);
}

/// Emit a new function and add it to the MLIR module.
Expand Down Expand Up @@ -166,7 +166,7 @@ class MLIRGenImpl {
if (!entryBlock.empty())
returnOp = dyn_cast<ReturnOp>(entryBlock.back());
if (!returnOp) {
builder.create<ReturnOp>(loc(funcAST.getProto()->loc()));
ReturnOp::create(builder, loc(funcAST.getProto()->loc()));
} else if (returnOp.hasOperand()) {
// Otherwise, if this return operation has an operand then add a result to
// the function.
Expand Down Expand Up @@ -202,9 +202,9 @@ class MLIRGenImpl {
// support '+' and '*'.
switch (binop.getOp()) {
case '+':
return builder.create<AddOp>(location, lhs, rhs);
return AddOp::create(builder, location, lhs, rhs);
case '*':
return builder.create<MulOp>(location, lhs, rhs);
return MulOp::create(builder, location, lhs, rhs);
}

emitError(location, "invalid binary operator '") << binop.getOp() << "'";
Expand Down Expand Up @@ -235,8 +235,8 @@ class MLIRGenImpl {
}

// Otherwise, this return operation has zero operands.
builder.create<ReturnOp>(location,
expr ? ArrayRef(expr) : ArrayRef<mlir::Value>());
ReturnOp::create(builder, location,
expr ? ArrayRef(expr) : ArrayRef<mlir::Value>());
return mlir::success();
}

Expand Down Expand Up @@ -264,8 +264,7 @@ class MLIRGenImpl {
// The attribute is a vector with a floating point value per element
// (number) in the array, see `collectData()` below for more details.
std::vector<double> data;
data.reserve(std::accumulate(lit.getDims().begin(), lit.getDims().end(), 1,
std::multiplies<int>()));
data.reserve(llvm::product_of(lit.getDims()));
collectData(lit, data);

// The type of this attribute is tensor of 64-bit floating-point with the
Expand All @@ -280,7 +279,7 @@ class MLIRGenImpl {

// Build the MLIR op `toy.constant`. This invokes the `ConstantOp::build`
// method.
return builder.create<ConstantOp>(loc(lit.loc()), type, dataAttribute);
return ConstantOp::create(builder, loc(lit.loc()), type, dataAttribute);
}

/// Recursive helper function to accumulate the data that compose an array
Expand Down Expand Up @@ -325,13 +324,13 @@ class MLIRGenImpl {
"does not accept multiple arguments");
return nullptr;
}
return builder.create<TransposeOp>(location, operands[0]);
return TransposeOp::create(builder, location, operands[0]);
}

// Otherwise this is a call to a user-defined function. Calls to
// user-defined functions are mapped to a custom call that takes the callee
// name as an attribute.
return builder.create<GenericCallOp>(location, callee, operands);
return GenericCallOp::create(builder, location, callee, operands);
}

/// Emit a print expression. It emits specific operations for two builtins:
Expand All @@ -341,13 +340,13 @@ class MLIRGenImpl {
if (!arg)
return mlir::failure();

builder.create<PrintOp>(loc(call.loc()), arg);
PrintOp::create(builder, loc(call.loc()), arg);
return mlir::success();
}

/// Emit a constant for a single number (FIXME: semantic? broadcast?)
mlir::Value mlirGen(NumberExprAST &num) {
return builder.create<ConstantOp>(loc(num.loc()), num.getValue());
return ConstantOp::create(builder, loc(num.loc()), num.getValue());
}

/// Dispatch codegen for the right expression subclass using RTTI.
Expand Down Expand Up @@ -391,8 +390,8 @@ class MLIRGenImpl {
// with specific shape, we emit a "reshape" operation. It will get
// optimized out later as needed.
if (!vardecl.getType().shape.empty()) {
value = builder.create<ReshapeOp>(loc(vardecl.loc()),
getType(vardecl.getType()), value);
value = ReshapeOp::create(builder, loc(vardecl.loc()),
getType(vardecl.getType()), value);
}

// Register the value in the symbol table.
Expand Down
2 changes: 1 addition & 1 deletion mlir/example/Ch3/parser/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void ASTDumper::dump(NumberExprAST *num) {
/// [ [ 1, 2 ], [ 3, 4 ] ]
/// We print out such array with the dimensions spelled out at every level:
/// <2,2>[<2>[ 1, 2 ], <2>[ 3, 4 ] ]
void printLitHelper(ExprAST *litOrNum) {
static void printLitHelper(ExprAST *litOrNum) {
// Inside a literal expression we can have either a number or another literal
if (auto *num = llvm::dyn_cast<NumberExprAST>(litOrNum)) {
llvm::errs() << num->getValue();
Expand Down
11 changes: 6 additions & 5 deletions mlir/example/Ch3/toyc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ static cl::opt<enum Action> emitAction(
static cl::opt<bool> enableOpt("opt", cl::desc("Enable optimizations"));

/// Returns a Toy AST resulting from parsing the file or a nullptr on error.
std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
static std::unique_ptr<toy::ModuleAST>
parseInputFile(llvm::StringRef filename) {
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileOrErr =
llvm::MemoryBuffer::getFileOrSTDIN(filename);
if (std::error_code ec = fileOrErr.getError()) {
Expand All @@ -77,8 +78,8 @@ std::unique_ptr<toy::ModuleAST> parseInputFile(llvm::StringRef filename) {
return parser.parseModule();
}

int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
mlir::OwningOpRef<mlir::ModuleOp> &module) {
static int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
mlir::OwningOpRef<mlir::ModuleOp> &module) {
// Handle '.toy' input to the compiler.
if (inputType != InputType::MLIR &&
!llvm::StringRef(inputFilename).ends_with(".mlir")) {
Expand Down Expand Up @@ -107,7 +108,7 @@ int loadMLIR(llvm::SourceMgr &sourceMgr, mlir::MLIRContext &context,
return 0;
}

int dumpMLIR() {
static int dumpMLIR() {
mlir::MLIRContext context;
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();
Expand All @@ -134,7 +135,7 @@ int dumpMLIR() {
return 0;
}

int dumpAST() {
static int dumpAST() {
if (inputType == InputType::MLIR) {
llvm::errs() << "Can't dump a Toy AST when the input is MLIR\n";
return 5;
Expand Down
1 change: 1 addition & 0 deletions mlir/example/Ch4/include/toy/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "llvm/ADT/StringRef.h"

#include <cstdlib>
#include <memory>
#include <string>

Expand Down
Loading
Loading