-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][ods] Allow sharding of op definitions #89423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// RUN: mlir-tblgen -gen-op-defs -op-shard-count=2 -I %S/../../include %s | FileCheck %s --check-prefix=DEFS | ||
// RUN: mlir-tblgen -gen-op-decls -op-shard-count=2 -I %S/../../include %s | FileCheck %s --check-prefix=DECLS | ||
|
||
include "mlir/IR/OpBase.td" | ||
|
||
def Test_Dialect : Dialect { | ||
let name = "test"; | ||
let cppNamespace = "test"; | ||
} | ||
|
||
class Test_Op<string mnemonic, list<Trait> traits = []> | ||
: Op<Test_Dialect, mnemonic, traits>; | ||
|
||
def OpA : Test_Op<"a">; | ||
def OpB : Test_Op<"b">; | ||
def OpC : Test_Op<"c">; | ||
|
||
// DECLS: OpA | ||
// DECLS: OpB | ||
// DECLS: OpC | ||
// DECLS: registerTestDialectOperations( | ||
// DECLS: registerTestDialectOperations0( | ||
// DECLS: registerTestDialectOperations1( | ||
|
||
// DEFS-LABEL: GET_OP_DEFS_0 | ||
// DEFS: void test::registerTestDialectOperations( | ||
// DEFS: void test::registerTestDialectOperations0( | ||
// DEFS: OpAAdaptor | ||
// DEFS: OpBAdaptor | ||
|
||
// DEFS-LABEL: GET_OP_DEFS_1 | ||
// DEFS: void test::registerTestDialectOperations1( | ||
// DEFS: OpCAdaptor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
set(LLVM_LINK_COMPONENTS Support) | ||
set(LIBS MLIRSupport) | ||
|
||
add_tablegen(mlir-src-sharder MLIR_SRC_SHARDER | ||
mlir-src-sharder.cpp | ||
|
||
DEPENDS | ||
${LIBS} | ||
) | ||
|
||
set_target_properties(mlir-src-sharder PROPERTIES FOLDER "Tablegenning") | ||
target_link_libraries(mlir-src-sharder PRIVATE ${LIBS}) | ||
|
||
mlir_check_all_link_libraries(mlir-src-sharder) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
//===- mlir-src-sharder.cpp - A tool for sharder generated source files ---===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Support/FileUtilities.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/InitLLVM.h" | ||
#include "llvm/Support/MemoryBuffer.h" | ||
#include "llvm/Support/ToolOutputFile.h" | ||
|
||
using namespace mlir; | ||
|
||
/// Create a dependency file for `-d` option. | ||
/// | ||
/// This functionality is generally only for the benefit of the build system, | ||
/// and is modeled after the same option in TableGen. | ||
static LogicalResult createDependencyFile(StringRef outputFilename, | ||
StringRef dependencyFile) { | ||
if (outputFilename == "-") { | ||
llvm::errs() << "error: the option -d must be used together with -o\n"; | ||
return failure(); | ||
} | ||
|
||
std::string errorMessage; | ||
std::unique_ptr<llvm::ToolOutputFile> outputFile = | ||
openOutputFile(dependencyFile, &errorMessage); | ||
if (!outputFile) { | ||
llvm::errs() << errorMessage << "\n"; | ||
return failure(); | ||
} | ||
|
||
outputFile->os() << outputFilename << ":\n"; | ||
outputFile->keep(); | ||
return success(); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
// FIXME: This is necessary because we link in TableGen, which defines its | ||
// options as static variables.. some of which overlap with our options. | ||
llvm::cl::ResetCommandLineParser(); | ||
|
||
llvm::cl::opt<unsigned> opShardIndex( | ||
"op-shard-index", llvm::cl::desc("The current shard index")); | ||
llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional, | ||
llvm::cl::desc("<input file>"), | ||
llvm::cl::init("-")); | ||
llvm::cl::opt<std::string> outputFilename( | ||
"o", llvm::cl::desc("Output filename"), llvm::cl::value_desc("filename"), | ||
llvm::cl::init("-")); | ||
llvm::cl::list<std::string> includeDirs( | ||
"I", llvm::cl::desc("Directory of include files"), | ||
llvm::cl::value_desc("directory"), llvm::cl::Prefix); | ||
llvm::cl::opt<std::string> dependencyFilename( | ||
"d", llvm::cl::desc("Dependency filename"), | ||
llvm::cl::value_desc("filename"), llvm::cl::init("")); | ||
llvm::cl::opt<bool> writeIfChanged( | ||
"write-if-changed", | ||
llvm::cl::desc("Only write to the output file if it changed")); | ||
|
||
llvm::InitLLVM y(argc, argv); | ||
llvm::cl::ParseCommandLineOptions(argc, argv); | ||
|
||
// Open the input file. | ||
std::string errorMessage; | ||
std::unique_ptr<llvm::MemoryBuffer> inputFile = | ||
openInputFile(inputFilename, &errorMessage); | ||
if (!inputFile) { | ||
llvm::errs() << errorMessage << "\n"; | ||
return 1; | ||
} | ||
|
||
// Write the output to a buffer. | ||
std::string outputStr; | ||
llvm::raw_string_ostream os(outputStr); | ||
os << "#define GET_OP_DEFS_" << opShardIndex << "\n" | ||
<< inputFile->getBuffer(); | ||
|
||
// Determine whether we need to write the output file. | ||
bool shouldWriteOutput = true; | ||
if (writeIfChanged) { | ||
// Only update the real output file if there are any differences. This | ||
// prevents recompilation of all the files depending on it if there aren't | ||
// any. | ||
if (auto existingOrErr = | ||
llvm::MemoryBuffer::getFile(outputFilename, /*IsText=*/true)) | ||
if (std::move(existingOrErr.get())->getBuffer() == os.str()) | ||
shouldWriteOutput = false; | ||
} | ||
|
||
// Populate the output file if necessary. | ||
if (shouldWriteOutput) { | ||
std::unique_ptr<llvm::ToolOutputFile> outputFile = | ||
openOutputFile(outputFilename, &errorMessage); | ||
if (!outputFile) { | ||
llvm::errs() << errorMessage << "\n"; | ||
return 1; | ||
} | ||
outputFile->os() << os.str(); | ||
outputFile->keep(); | ||
} | ||
|
||
// Always write the depfile, even if the main output hasn't changed. If it's | ||
// missing, Ninja considers the output dirty. | ||
if (!dependencyFilename.empty()) | ||
if (failed(createDependencyFile(outputFilename, dependencyFilename))) | ||
return 1; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MLIR src sharded makes me think it's related to . mlir files rather than ODS ones. Did you consider making this a mlir-tblgen "function" (such as attribute gen or doc gen) and then call it 2x?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mlir-tblgen
only ingests.td
files as records. Do you want me to inject a hook into its main function to sniff the command and change its operating mode?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't follow: can you expand on the command line issue with mlir-tblgen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mlir-tblgen
calls into the TableGen parser and then calls into a function based on the command line with the parsed records. In order to make it ingest a C++ file (or another kind of file), I have to intercept it in the main function:Turn this
Into this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joker-eph ping!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping should be for @jpienaar who started in this direction first :)
I agree though that using mlir-tblgen for non-tablegen file does not seem right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wasn't resolved?