2018-06-22 00:49:33 +08:00
|
|
|
//===- mlir-opt.cpp - MLIR Optimizer Driver -------------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This is a command line utility that parses an MLIR file, runs an optimization
|
|
|
|
// pass, then prints the result back out. It is designed to support unit
|
|
|
|
// testing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-06 12:12:29 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-07-25 11:01:16 +08:00
|
|
|
#include "mlir/IR/MLFunction.h"
|
2018-06-23 13:03:48 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2018-06-22 06:22:42 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2018-06-23 01:39:19 +08:00
|
|
|
#include "mlir/Parser.h"
|
2018-07-30 03:37:35 +08:00
|
|
|
#include "mlir/TensorFlow/ControlFlowOps.h"
|
|
|
|
#include "mlir/TensorFlow/Passes.h"
|
2018-08-14 08:25:13 +08:00
|
|
|
#include "mlir/Transforms/Pass.h"
|
2018-07-30 03:37:35 +08:00
|
|
|
#include "mlir/Transforms/Passes.h"
|
2018-06-22 00:49:33 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2018-06-22 06:22:42 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2018-06-22 00:49:33 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2018-08-06 12:12:29 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2018-06-25 23:10:46 +08:00
|
|
|
#include "llvm/Support/Regex.h"
|
2018-07-08 10:12:22 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2018-06-22 06:22:42 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2018-08-31 08:35:15 +08:00
|
|
|
|
2018-06-22 00:49:33 +08:00
|
|
|
using namespace mlir;
|
2018-06-22 06:22:42 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
outputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
checkParserErrors("check-parser-errors", cl::desc("Check for parser errors"),
|
|
|
|
cl::init(false));
|
2018-06-22 06:22:42 +08:00
|
|
|
|
2018-07-30 05:13:03 +08:00
|
|
|
enum Passes {
|
|
|
|
ConvertToCFG,
|
Extend loop unrolling to unroll by a given factor; add builder for affine
apply op.
- add builder for AffineApplyOp (first one for an operation that has
non-zero operands)
- add support for loop unrolling by a given factor; uses the affine apply op
builder.
While on this, change 'step' of ForStmt to be 'unsigned' instead of
AffineConstantExpr *. Add setters for ForStmt lb, ub, step.
Sample Input:
// CHECK-LABEL: mlfunc @loop_nest_unroll_cleanup() {
mlfunc @loop_nest_unroll_cleanup() {
for %i = 1 to 100 {
for %j = 0 to 17 {
%x = "addi32"(%j, %j) : (affineint, affineint) -> i32
%y = "addi32"(%x, %x) : (i32, i32) -> i32
}
}
return
}
Output:
$ mlir-opt -loop-unroll -unroll-factor=4 /tmp/single2.mlir
#map0 = (d0) -> (d0 + 1)
#map1 = (d0) -> (d0 + 2)
#map2 = (d0) -> (d0 + 3)
mlfunc @loop_nest_unroll_cleanup() {
for %i0 = 1 to 100 {
for %i1 = 0 to 17 step 4 {
%0 = "addi32"(%i1, %i1) : (affineint, affineint) -> i32
%1 = "addi32"(%0, %0) : (i32, i32) -> i32
%2 = affine_apply #map0(%i1)
%3 = "addi32"(%2, %2) : (affineint, affineint) -> i32
%4 = affine_apply #map1(%i1)
%5 = "addi32"(%4, %4) : (affineint, affineint) -> i32
%6 = affine_apply #map2(%i1)
%7 = "addi32"(%6, %6) : (affineint, affineint) -> i32
}
for %i2 = 16 to 17 {
%8 = "addi32"(%i2, %i2) : (affineint, affineint) -> i32
%9 = "addi32"(%8, %8) : (i32, i32) -> i32
}
}
return
}
PiperOrigin-RevId: 209676220
2018-08-22 07:01:23 +08:00
|
|
|
LoopUnroll,
|
2018-08-29 09:24:27 +08:00
|
|
|
LoopUnrollAndJam,
|
2018-08-31 08:35:15 +08:00
|
|
|
SimplifyAffineExpr,
|
2018-07-30 05:13:03 +08:00
|
|
|
TFRaiseControlFlow,
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::list<Passes> passList(
|
|
|
|
"", cl::desc("Compiler passes to run"),
|
|
|
|
cl::values(clEnumValN(ConvertToCFG, "convert-to-cfg",
|
|
|
|
"Convert all ML functions in the module to CFG ones"),
|
Extend loop unrolling to unroll by a given factor; add builder for affine
apply op.
- add builder for AffineApplyOp (first one for an operation that has
non-zero operands)
- add support for loop unrolling by a given factor; uses the affine apply op
builder.
While on this, change 'step' of ForStmt to be 'unsigned' instead of
AffineConstantExpr *. Add setters for ForStmt lb, ub, step.
Sample Input:
// CHECK-LABEL: mlfunc @loop_nest_unroll_cleanup() {
mlfunc @loop_nest_unroll_cleanup() {
for %i = 1 to 100 {
for %j = 0 to 17 {
%x = "addi32"(%j, %j) : (affineint, affineint) -> i32
%y = "addi32"(%x, %x) : (i32, i32) -> i32
}
}
return
}
Output:
$ mlir-opt -loop-unroll -unroll-factor=4 /tmp/single2.mlir
#map0 = (d0) -> (d0 + 1)
#map1 = (d0) -> (d0 + 2)
#map2 = (d0) -> (d0 + 3)
mlfunc @loop_nest_unroll_cleanup() {
for %i0 = 1 to 100 {
for %i1 = 0 to 17 step 4 {
%0 = "addi32"(%i1, %i1) : (affineint, affineint) -> i32
%1 = "addi32"(%0, %0) : (i32, i32) -> i32
%2 = affine_apply #map0(%i1)
%3 = "addi32"(%2, %2) : (affineint, affineint) -> i32
%4 = affine_apply #map1(%i1)
%5 = "addi32"(%4, %4) : (affineint, affineint) -> i32
%6 = affine_apply #map2(%i1)
%7 = "addi32"(%6, %6) : (affineint, affineint) -> i32
}
for %i2 = 16 to 17 {
%8 = "addi32"(%i2, %i2) : (affineint, affineint) -> i32
%9 = "addi32"(%8, %8) : (i32, i32) -> i32
}
}
return
}
PiperOrigin-RevId: 209676220
2018-08-22 07:01:23 +08:00
|
|
|
clEnumValN(LoopUnroll, "loop-unroll", "Unroll loops"),
|
2018-08-29 09:24:27 +08:00
|
|
|
clEnumValN(LoopUnrollAndJam, "loop-unroll-jam",
|
|
|
|
"Unroll and jam loops"),
|
2018-08-31 08:35:15 +08:00
|
|
|
clEnumValN(SimplifyAffineExpr, "simplify-affine-expr",
|
|
|
|
"Simplify affine expressions"),
|
2018-07-30 05:13:03 +08:00
|
|
|
clEnumValN(TFRaiseControlFlow, "tf-raise-control-flow",
|
|
|
|
"Dynamic TensorFlow Switch/Match nodes to a CFG")));
|
2018-07-30 03:37:35 +08:00
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
enum OptResult { OptSuccess, OptFailure };
|
|
|
|
|
2018-06-22 06:22:42 +08:00
|
|
|
/// Open the specified output file and return it, exiting if there is any I/O or
|
|
|
|
/// other errors.
|
|
|
|
static std::unique_ptr<ToolOutputFile> getOutputStream() {
|
|
|
|
std::error_code error;
|
2018-07-31 06:00:47 +08:00
|
|
|
auto result =
|
|
|
|
llvm::make_unique<ToolOutputFile>(outputFilename, error, sys::fs::F_None);
|
2018-06-22 06:22:42 +08:00
|
|
|
if (error) {
|
|
|
|
llvm::errs() << error.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-08-17 08:26:08 +08:00
|
|
|
// The function to initialize the MLIRContext for different ops is defined in
|
|
|
|
// another compilation unit to allow different tests to link in different
|
|
|
|
// context initializations (e.g., op registrations).
|
|
|
|
extern void initializeMLIRContext(MLIRContext *ctx);
|
2018-07-30 03:37:35 +08:00
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
/// Parses the memory buffer and, if successfully parsed, prints the parsed
|
2018-07-25 01:15:13 +08:00
|
|
|
/// output. Optionally, convert ML functions into CFG functions.
|
|
|
|
/// TODO: pull parsing and printing into separate functions.
|
2018-07-04 04:24:09 +08:00
|
|
|
OptResult parseAndPrintMemoryBuffer(std::unique_ptr<MemoryBuffer> buffer) {
|
2018-06-25 00:10:36 +08:00
|
|
|
// Tell sourceMgr about this buffer, which is what the parser will pick up.
|
|
|
|
SourceMgr sourceMgr;
|
|
|
|
sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-06-25 10:17:35 +08:00
|
|
|
// Parse the input file.
|
2018-06-23 13:03:48 +08:00
|
|
|
MLIRContext context;
|
2018-08-17 08:26:08 +08:00
|
|
|
initializeMLIRContext(&context);
|
2018-07-04 04:24:09 +08:00
|
|
|
std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
|
|
|
|
if (!module)
|
|
|
|
return OptFailure;
|
2018-06-25 00:10:36 +08:00
|
|
|
|
2018-07-30 05:13:03 +08:00
|
|
|
// Run each of the passes that were selected.
|
|
|
|
for (auto passKind : passList) {
|
|
|
|
Pass *pass = nullptr;
|
|
|
|
switch (passKind) {
|
|
|
|
case ConvertToCFG:
|
|
|
|
pass = createConvertToCFGPass();
|
|
|
|
break;
|
Extend loop unrolling to unroll by a given factor; add builder for affine
apply op.
- add builder for AffineApplyOp (first one for an operation that has
non-zero operands)
- add support for loop unrolling by a given factor; uses the affine apply op
builder.
While on this, change 'step' of ForStmt to be 'unsigned' instead of
AffineConstantExpr *. Add setters for ForStmt lb, ub, step.
Sample Input:
// CHECK-LABEL: mlfunc @loop_nest_unroll_cleanup() {
mlfunc @loop_nest_unroll_cleanup() {
for %i = 1 to 100 {
for %j = 0 to 17 {
%x = "addi32"(%j, %j) : (affineint, affineint) -> i32
%y = "addi32"(%x, %x) : (i32, i32) -> i32
}
}
return
}
Output:
$ mlir-opt -loop-unroll -unroll-factor=4 /tmp/single2.mlir
#map0 = (d0) -> (d0 + 1)
#map1 = (d0) -> (d0 + 2)
#map2 = (d0) -> (d0 + 3)
mlfunc @loop_nest_unroll_cleanup() {
for %i0 = 1 to 100 {
for %i1 = 0 to 17 step 4 {
%0 = "addi32"(%i1, %i1) : (affineint, affineint) -> i32
%1 = "addi32"(%0, %0) : (i32, i32) -> i32
%2 = affine_apply #map0(%i1)
%3 = "addi32"(%2, %2) : (affineint, affineint) -> i32
%4 = affine_apply #map1(%i1)
%5 = "addi32"(%4, %4) : (affineint, affineint) -> i32
%6 = affine_apply #map2(%i1)
%7 = "addi32"(%6, %6) : (affineint, affineint) -> i32
}
for %i2 = 16 to 17 {
%8 = "addi32"(%i2, %i2) : (affineint, affineint) -> i32
%9 = "addi32"(%8, %8) : (i32, i32) -> i32
}
}
return
}
PiperOrigin-RevId: 209676220
2018-08-22 07:01:23 +08:00
|
|
|
case LoopUnroll:
|
2018-08-29 09:24:27 +08:00
|
|
|
pass = createLoopUnrollPass();
|
|
|
|
break;
|
|
|
|
case LoopUnrollAndJam:
|
|
|
|
pass = createLoopUnrollAndJamPass();
|
2018-08-07 09:40:34 +08:00
|
|
|
break;
|
2018-08-31 08:35:15 +08:00
|
|
|
case SimplifyAffineExpr:
|
|
|
|
pass = createSimplifyAffineExprPass();
|
|
|
|
break;
|
2018-07-30 05:13:03 +08:00
|
|
|
case TFRaiseControlFlow:
|
|
|
|
pass = createRaiseTFControlFlowPass();
|
|
|
|
break;
|
|
|
|
}
|
2018-07-30 03:37:35 +08:00
|
|
|
|
|
|
|
pass->runOnModule(module.get());
|
|
|
|
delete pass;
|
|
|
|
module->verify();
|
2018-07-25 11:01:16 +08:00
|
|
|
}
|
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
// Print the output.
|
|
|
|
auto output = getOutputStream();
|
|
|
|
module->print(output->os());
|
|
|
|
output->keep();
|
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
return OptSuccess;
|
2018-06-25 00:10:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Split the memory buffer into multiple buffers using the marker -----.
|
2018-07-04 04:24:09 +08:00
|
|
|
OptResult
|
|
|
|
splitMemoryBufferForErrorChecking(std::unique_ptr<MemoryBuffer> buffer) {
|
2018-06-25 00:10:36 +08:00
|
|
|
const char marker[] = "-----";
|
|
|
|
SmallVector<StringRef, 2> sourceBuffers;
|
|
|
|
buffer->getBuffer().split(sourceBuffers, marker);
|
|
|
|
|
2018-06-25 23:10:46 +08:00
|
|
|
// Error reporter that verifies error reports matches expected error
|
|
|
|
// substring.
|
|
|
|
// TODO: Only checking for error cases below. Could be expanded to other kinds
|
|
|
|
// of diagnostics.
|
|
|
|
// TODO: Enable specifying errors on different lines (@-1).
|
|
|
|
// TODO: Currently only checking if substring matches, enable regex checking.
|
2018-07-04 04:24:09 +08:00
|
|
|
OptResult opt_result = OptSuccess;
|
2018-06-26 23:56:55 +08:00
|
|
|
SourceMgr fileSourceMgr;
|
|
|
|
fileSourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
|
|
|
|
|
2018-07-08 10:12:22 +08:00
|
|
|
// Record the expected errors's position, substring and whether it was seen.
|
|
|
|
struct ExpectedError {
|
|
|
|
int lineNo;
|
|
|
|
StringRef substring;
|
|
|
|
SMLoc fileLoc;
|
|
|
|
bool matched;
|
|
|
|
};
|
|
|
|
|
2018-06-26 23:56:55 +08:00
|
|
|
// Tracks offset of subbuffer into original buffer.
|
|
|
|
const char *fileOffset =
|
|
|
|
fileSourceMgr.getMemoryBuffer(fileSourceMgr.getMainFileID())
|
|
|
|
->getBufferStart();
|
|
|
|
|
2018-07-08 10:12:22 +08:00
|
|
|
for (auto &subbuffer : sourceBuffers) {
|
2018-06-26 23:56:55 +08:00
|
|
|
SourceMgr sourceMgr;
|
|
|
|
// Tell sourceMgr about this buffer, which is what the parser will pick up.
|
2018-08-21 23:42:19 +08:00
|
|
|
sourceMgr.AddNewSourceBuffer(MemoryBuffer::getMemBufferCopy(subbuffer),
|
|
|
|
SMLoc());
|
2018-06-26 23:56:55 +08:00
|
|
|
|
2018-08-06 12:12:29 +08:00
|
|
|
// Extract the expected errors.
|
2018-08-21 23:42:19 +08:00
|
|
|
llvm::Regex expected("expected-error *(@[+-][0-9]+)? *{{(.*)}}");
|
2018-07-08 10:12:22 +08:00
|
|
|
SmallVector<ExpectedError, 2> expectedErrors;
|
|
|
|
SmallVector<StringRef, 100> lines;
|
|
|
|
subbuffer.split(lines, '\n');
|
|
|
|
size_t bufOffset = 0;
|
|
|
|
for (int lineNo = 0; lineNo < lines.size(); ++lineNo) {
|
|
|
|
SmallVector<StringRef, 3> matches;
|
|
|
|
if (expected.match(lines[lineNo], &matches)) {
|
|
|
|
// Point to the start of expected-error.
|
|
|
|
SMLoc errorStart =
|
|
|
|
SMLoc::getFromPointer(fileOffset + bufOffset +
|
|
|
|
lines[lineNo].size() - matches[2].size() - 2);
|
|
|
|
ExpectedError expErr{lineNo + 1, matches[2], errorStart, false};
|
|
|
|
int offset;
|
|
|
|
if (!matches[1].empty() &&
|
|
|
|
!matches[1].drop_front().getAsInteger(0, offset)) {
|
|
|
|
expErr.lineNo += offset;
|
|
|
|
}
|
|
|
|
expectedErrors.push_back(expErr);
|
|
|
|
}
|
|
|
|
bufOffset += lines[lineNo].size() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error checker that verifies reported error was expected.
|
|
|
|
auto checker = [&](const SMDiagnostic &err) {
|
|
|
|
for (auto &e : expectedErrors) {
|
|
|
|
if (err.getLineNo() == e.lineNo &&
|
|
|
|
err.getMessage().contains(e.substring)) {
|
|
|
|
e.matched = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Report error if no match found.
|
|
|
|
const auto &sourceMgr = *err.getSourceMgr();
|
|
|
|
const char *bufferStart =
|
|
|
|
sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID())
|
|
|
|
->getBufferStart();
|
|
|
|
|
|
|
|
size_t offset = err.getLoc().getPointer() - bufferStart;
|
|
|
|
SMLoc loc = SMLoc::getFromPointer(fileOffset + offset);
|
2018-06-26 23:56:55 +08:00
|
|
|
fileSourceMgr.PrintMessage(loc, SourceMgr::DK_Error,
|
2018-07-08 10:12:22 +08:00
|
|
|
"unexpected error: " + err.getMessage());
|
2018-07-04 04:24:09 +08:00
|
|
|
opt_result = OptFailure;
|
2018-07-08 10:12:22 +08:00
|
|
|
};
|
2018-06-25 23:10:46 +08:00
|
|
|
|
2018-06-26 23:56:55 +08:00
|
|
|
// Parse the input file.
|
|
|
|
MLIRContext context;
|
2018-08-17 08:26:08 +08:00
|
|
|
initializeMLIRContext(&context);
|
2018-06-26 23:56:55 +08:00
|
|
|
std::unique_ptr<Module> module(
|
|
|
|
parseSourceFile(sourceMgr, &context, checker));
|
2018-07-08 10:12:22 +08:00
|
|
|
|
|
|
|
// Verify that all expected errors were seen.
|
|
|
|
for (auto err : expectedErrors) {
|
|
|
|
if (!err.matched) {
|
|
|
|
SMRange range(err.fileLoc,
|
|
|
|
SMLoc::getFromPointer(err.fileLoc.getPointer() +
|
|
|
|
err.substring.size()));
|
|
|
|
fileSourceMgr.PrintMessage(
|
|
|
|
err.fileLoc, SourceMgr::DK_Error,
|
|
|
|
"expected error \"" + err.substring + "\" was not produced", range);
|
|
|
|
opt_result = OptFailure;
|
|
|
|
}
|
2018-06-25 23:10:46 +08:00
|
|
|
}
|
2018-06-26 23:56:55 +08:00
|
|
|
|
|
|
|
fileOffset += subbuffer.size() + strlen(marker);
|
2018-06-25 23:10:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
return opt_result;
|
2018-06-25 00:10:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2018-08-06 12:12:29 +08:00
|
|
|
llvm::PrettyStackTraceProgram x(argc, argv);
|
|
|
|
InitLLVM y(argc, argv);
|
2018-06-23 13:03:48 +08:00
|
|
|
|
2018-06-22 06:22:42 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-06-23 01:39:19 +08:00
|
|
|
// Set up the input file.
|
|
|
|
auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename);
|
|
|
|
if (std::error_code error = fileOrErr.getError()) {
|
|
|
|
llvm::errs() << argv[0] << ": could not open input file '" << inputFilename
|
|
|
|
<< "': " << error.message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-06-25 00:10:36 +08:00
|
|
|
if (checkParserErrors)
|
2018-07-04 04:24:09 +08:00
|
|
|
return splitMemoryBufferForErrorChecking(std::move(*fileOrErr));
|
2018-06-25 23:10:46 +08:00
|
|
|
|
2018-07-04 04:24:09 +08:00
|
|
|
return parseAndPrintMemoryBuffer(std::move(*fileOrErr));
|
2018-06-22 00:49:33 +08:00
|
|
|
}
|