From f56494f537dcee9324814c65b462e07666d74084 Mon Sep 17 00:00:00 2001 From: River Riddle Date: Wed, 7 Aug 2019 19:23:35 -0700 Subject: [PATCH] NFC: Update FuncOp::addEntryBlock to return the newly inserted block. The entry block is often used recently after insertion. This removes the need to perform an additional lookup in such cases. PiperOrigin-RevId: 262265671 --- mlir/examples/toy/Ch2/mlir/MLIRGen.cpp | 4 +--- mlir/examples/toy/Ch3/mlir/MLIRGen.cpp | 4 +--- mlir/examples/toy/Ch4/mlir/MLIRGen.cpp | 4 +--- mlir/examples/toy/Ch5/mlir/MLIRGen.cpp | 4 +--- mlir/include/mlir/IR/Function.h | 5 +++-- mlir/lib/Dialect/SPIRV/Serialization/ConvertFromBinary.cpp | 3 +-- mlir/lib/IR/Function.cpp | 3 ++- mlir/lib/Linalg/Transforms/LowerToLLVMDialect.cpp | 4 +--- 8 files changed, 11 insertions(+), 20 deletions(-) diff --git a/mlir/examples/toy/Ch2/mlir/MLIRGen.cpp b/mlir/examples/toy/Ch2/mlir/MLIRGen.cpp index e3d3ac3..7b874b9 100644 --- a/mlir/examples/toy/Ch2/mlir/MLIRGen.cpp +++ b/mlir/examples/toy/Ch2/mlir/MLIRGen.cpp @@ -162,9 +162,7 @@ private: // Let's start the body of the function now! // In MLIR the entry block of the function is special: it must have the same // argument list as the function itself. - function.addEntryBlock(); - - auto &entryBlock = function.front(); + auto &entryBlock = *function.addEntryBlock(); auto &protoArgs = funcAST.getProto()->getArgs(); // Declare all the function arguments in the symbol table. for (const auto &name_value : diff --git a/mlir/examples/toy/Ch3/mlir/MLIRGen.cpp b/mlir/examples/toy/Ch3/mlir/MLIRGen.cpp index f0479a6..e3b06a7 100644 --- a/mlir/examples/toy/Ch3/mlir/MLIRGen.cpp +++ b/mlir/examples/toy/Ch3/mlir/MLIRGen.cpp @@ -163,9 +163,7 @@ private: // Let's start the body of the function now! // In MLIR the entry block of the function is special: it must have the same // argument list as the function itself. - function.addEntryBlock(); - - auto &entryBlock = function.front(); + auto &entryBlock = *function.addEntryBlock(); auto &protoArgs = funcAST.getProto()->getArgs(); // Declare all the function arguments in the symbol table. for (const auto &name_value : diff --git a/mlir/examples/toy/Ch4/mlir/MLIRGen.cpp b/mlir/examples/toy/Ch4/mlir/MLIRGen.cpp index ce67eb1..e61d1aa 100644 --- a/mlir/examples/toy/Ch4/mlir/MLIRGen.cpp +++ b/mlir/examples/toy/Ch4/mlir/MLIRGen.cpp @@ -163,9 +163,7 @@ private: // Let's start the body of the function now! // In MLIR the entry block of the function is special: it must have the same // argument list as the function itself. - function.addEntryBlock(); - - auto &entryBlock = function.front(); + auto &entryBlock = *function.addEntryBlock(); auto &protoArgs = funcAST.getProto()->getArgs(); // Declare all the function arguments in the symbol table. for (const auto &name_value : diff --git a/mlir/examples/toy/Ch5/mlir/MLIRGen.cpp b/mlir/examples/toy/Ch5/mlir/MLIRGen.cpp index c3c72b3..8d7d169 100644 --- a/mlir/examples/toy/Ch5/mlir/MLIRGen.cpp +++ b/mlir/examples/toy/Ch5/mlir/MLIRGen.cpp @@ -163,9 +163,7 @@ private: // Let's start the body of the function now! // In MLIR the entry block of the function is special: it must have the same // argument list as the function itself. - function.addEntryBlock(); - - auto &entryBlock = function.front(); + auto &entryBlock = *function.addEntryBlock(); auto &protoArgs = funcAST.getProto()->getArgs(); // Declare all the function arguments in the symbol table. for (const auto &name_value : diff --git a/mlir/include/mlir/IR/Function.h b/mlir/include/mlir/IR/Function.h index e0fb45b..73da52f 100644 --- a/mlir/include/mlir/IR/Function.h +++ b/mlir/include/mlir/IR/Function.h @@ -100,8 +100,9 @@ public: //===--------------------------------------------------------------------===// /// Add an entry block to an empty function, and set up the block arguments - /// to match the signature of the function. - void addEntryBlock(); + /// to match the signature of the function. The newly inserted entry block is + /// returned. + Block *addEntryBlock(); private: // This trait needs access to `getNumFuncArguments` and `verifyType` hooks diff --git a/mlir/lib/Dialect/SPIRV/Serialization/ConvertFromBinary.cpp b/mlir/lib/Dialect/SPIRV/Serialization/ConvertFromBinary.cpp index f5e795a..38e8d93 100644 --- a/mlir/lib/Dialect/SPIRV/Serialization/ConvertFromBinary.cpp +++ b/mlir/lib/Dialect/SPIRV/Serialization/ConvertFromBinary.cpp @@ -40,8 +40,7 @@ Block *createOneBlockFunction(Builder builder, ModuleOp module) { auto fn = FuncOp::create(builder.getUnknownLoc(), "spirv_module", fnType); module.push_back(fn); - fn.addEntryBlock(); - auto *block = &fn.front(); + auto *block = fn.addEntryBlock(); OpBuilder(block).create(builder.getUnknownLoc()); return block; diff --git a/mlir/lib/IR/Function.cpp b/mlir/lib/IR/Function.cpp index af0edf9..42bc03f 100644 --- a/mlir/lib/IR/Function.cpp +++ b/mlir/lib/IR/Function.cpp @@ -109,11 +109,12 @@ LogicalResult FuncOp::verify() { /// Add an entry block to an empty function, and set up the block arguments /// to match the signature of the function. -void FuncOp::addEntryBlock() { +Block *FuncOp::addEntryBlock() { assert(empty() && "function already has an entry block"); auto *entry = new Block(); push_back(entry); entry->addArguments(getType().getInputs()); + return entry; } /// Clone the internal blocks from this function into dest and all attributes diff --git a/mlir/lib/Linalg/Transforms/LowerToLLVMDialect.cpp b/mlir/lib/Linalg/Transforms/LowerToLLVMDialect.cpp index 7c2ea59..f06a09d 100644 --- a/mlir/lib/Linalg/Transforms/LowerToLLVMDialect.cpp +++ b/mlir/lib/Linalg/Transforms/LowerToLLVMDialect.cpp @@ -600,9 +600,7 @@ static void getLLVMLibraryCallDefinition(FuncOp fn, auto implFn = getLLVMLibraryCallImplDefinition(fn); // Generate the function body. - fn.addEntryBlock(); - - OpBuilder builder(fn.getBody()); + OpBuilder builder(fn.addEntryBlock()); edsc::ScopedContext scope(builder, fn.getLoc()); SmallVector implFnArgs; -- 2.7.4