|
| 1 | +//===- mlir-src-sharder.cpp - A tool for sharder generated source files ---===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "mlir/Support/FileUtilities.h" |
| 10 | +#include "mlir/Support/LogicalResult.h" |
| 11 | +#include "llvm/Support/CommandLine.h" |
| 12 | +#include "llvm/Support/InitLLVM.h" |
| 13 | +#include "llvm/Support/MemoryBuffer.h" |
| 14 | +#include "llvm/Support/ToolOutputFile.h" |
| 15 | + |
| 16 | +using namespace mlir; |
| 17 | + |
| 18 | +/// Create a dependency file for `-d` option. |
| 19 | +/// |
| 20 | +/// This functionality is generally only for the benefit of the build system, |
| 21 | +/// and is modeled after the same option in TableGen. |
| 22 | +static LogicalResult createDependencyFile(StringRef outputFilename, |
| 23 | + StringRef dependencyFile) { |
| 24 | + if (outputFilename == "-") { |
| 25 | + llvm::errs() << "error: the option -d must be used together with -o\n"; |
| 26 | + return failure(); |
| 27 | + } |
| 28 | + |
| 29 | + std::string errorMessage; |
| 30 | + std::unique_ptr<llvm::ToolOutputFile> outputFile = |
| 31 | + openOutputFile(dependencyFile, &errorMessage); |
| 32 | + if (!outputFile) { |
| 33 | + llvm::errs() << errorMessage << "\n"; |
| 34 | + return failure(); |
| 35 | + } |
| 36 | + |
| 37 | + outputFile->os() << outputFilename << ":\n"; |
| 38 | + outputFile->keep(); |
| 39 | + return success(); |
| 40 | +} |
| 41 | + |
| 42 | +int main(int argc, char **argv) { |
| 43 | + // FIXME: This is necessary because we link in TableGen, which defines its |
| 44 | + // options as static variables.. some of which overlap with our options. |
| 45 | + llvm::cl::ResetCommandLineParser(); |
| 46 | + |
| 47 | + llvm::cl::opt<unsigned> opShardIndex( |
| 48 | + "op-shard-index", llvm::cl::desc("The current shard index")); |
| 49 | + llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional, |
| 50 | + llvm::cl::desc("<input file>"), |
| 51 | + llvm::cl::init("-")); |
| 52 | + llvm::cl::opt<std::string> outputFilename( |
| 53 | + "o", llvm::cl::desc("Output filename"), llvm::cl::value_desc("filename"), |
| 54 | + llvm::cl::init("-")); |
| 55 | + llvm::cl::list<std::string> includeDirs( |
| 56 | + "I", llvm::cl::desc("Directory of include files"), |
| 57 | + llvm::cl::value_desc("directory"), llvm::cl::Prefix); |
| 58 | + llvm::cl::opt<std::string> dependencyFilename( |
| 59 | + "d", llvm::cl::desc("Dependency filename"), |
| 60 | + llvm::cl::value_desc("filename"), llvm::cl::init("")); |
| 61 | + llvm::cl::opt<bool> writeIfChanged( |
| 62 | + "write-if-changed", |
| 63 | + llvm::cl::desc("Only write to the output file if it changed")); |
| 64 | + |
| 65 | + llvm::InitLLVM y(argc, argv); |
| 66 | + llvm::cl::ParseCommandLineOptions(argc, argv); |
| 67 | + |
| 68 | + // Open the input file. |
| 69 | + std::string errorMessage; |
| 70 | + std::unique_ptr<llvm::MemoryBuffer> inputFile = |
| 71 | + openInputFile(inputFilename, &errorMessage); |
| 72 | + if (!inputFile) { |
| 73 | + llvm::errs() << errorMessage << "\n"; |
| 74 | + return 1; |
| 75 | + } |
| 76 | + |
| 77 | + // Write the output to a buffer. |
| 78 | + std::string outputStr; |
| 79 | + llvm::raw_string_ostream os(outputStr); |
| 80 | + os << "#define GET_OP_DEFS_" << opShardIndex << "\n" |
| 81 | + << inputFile->getBuffer(); |
| 82 | + |
| 83 | + // Determine whether we need to write the output file. |
| 84 | + bool shouldWriteOutput = true; |
| 85 | + if (writeIfChanged) { |
| 86 | + // Only update the real output file if there are any differences. This |
| 87 | + // prevents recompilation of all the files depending on it if there aren't |
| 88 | + // any. |
| 89 | + if (auto existingOrErr = |
| 90 | + llvm::MemoryBuffer::getFile(outputFilename, /*IsText=*/true)) |
| 91 | + if (std::move(existingOrErr.get())->getBuffer() == os.str()) |
| 92 | + shouldWriteOutput = false; |
| 93 | + } |
| 94 | + |
| 95 | + // Populate the output file if necessary. |
| 96 | + if (shouldWriteOutput) { |
| 97 | + std::unique_ptr<llvm::ToolOutputFile> outputFile = |
| 98 | + openOutputFile(outputFilename, &errorMessage); |
| 99 | + if (!outputFile) { |
| 100 | + llvm::errs() << errorMessage << "\n"; |
| 101 | + return 1; |
| 102 | + } |
| 103 | + outputFile->os() << os.str(); |
| 104 | + outputFile->keep(); |
| 105 | + } |
| 106 | + |
| 107 | + // Always write the depfile, even if the main output hasn't changed. If it's |
| 108 | + // missing, Ninja considers the output dirty. |
| 109 | + if (!dependencyFilename.empty()) |
| 110 | + if (failed(createDependencyFile(outputFilename, dependencyFilename))) |
| 111 | + return 1; |
| 112 | + |
| 113 | + return 0; |
| 114 | +} |
0 commit comments