-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#sdy add bytecode serialization utility
PiperOrigin-RevId: 730837065
- Loading branch information
1 parent
c77626b
commit 4807f30
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,81 @@ | ||
/* Copyright 2025 The Shardy 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. | ||
==============================================================================*/ | ||
|
||
// MLIR `translate` tool for allowing SDY dialect bytecode emission. | ||
// | ||
// Usage: | ||
// sdy_translate <file.mlir> -serialize | ||
// sdy_translate <file.mlir.bc> -deserialize | ||
|
||
#include "llvm/Support/CommandLine.h" | ||
#include "llvm/Support/LogicalResult.h" | ||
#include "mlir/Bytecode/BytecodeWriter.h" | ||
#include "mlir/Dialect/Func/Extensions/AllExtensions.h" | ||
#include "mlir/Dialect/Quant/IR/Quant.h" | ||
#include "mlir/IR/BuiltinOps.h" | ||
#include "mlir/IR/DialectRegistry.h" | ||
#include "mlir/IR/MLIRContext.h" | ||
#include "mlir/Parser/Parser.h" | ||
#include "mlir/Support/LLVM.h" | ||
#include "mlir/Tools/mlir-opt/MlirOptMain.h" | ||
#include "mlir/Tools/mlir-translate/MlirTranslateMain.h" | ||
#include "mlir/Tools/mlir-translate/Translation.h" | ||
#include "mlir/Transforms/Passes.h" | ||
#include "shardy/dialect/sdy/ir/dialect.h" | ||
#include "shardy/dialect/sdy/ir/register.h" | ||
|
||
namespace mlir { | ||
|
||
namespace { | ||
llvm::cl::opt<bool> stripDebuginfoOption( | ||
"strip-debuginfo", llvm::cl::desc("Strip debug info from all operations"), | ||
llvm::cl::init(false)); | ||
|
||
void registerDialectsForSdy(DialectRegistry ®istry) { | ||
mlir::sdy::registerAllDialects(registry); | ||
registry.insert<mlir::quant::QuantDialect>(); | ||
} | ||
} // namespace | ||
|
||
TranslateFromMLIRRegistration serializeRegistration( | ||
"serialize", "Serialize SDY program into a portable artifact", | ||
[](mlir::ModuleOp module, llvm::raw_ostream &os) -> llvm::LogicalResult { | ||
if (stripDebuginfoOption) { | ||
PassManager pm(module->getContext()); | ||
pm.addPass(createStripDebugInfoPass()); | ||
if (failed(pm.run(module))) | ||
return module.emitError("failed to strip debuginfo"); | ||
} | ||
const auto *producer = "SDY"; | ||
BytecodeWriterConfig writerConfig(producer); | ||
return writeBytecodeToFile(module, os, writerConfig); | ||
}, | ||
[](DialectRegistry ®istry) { registerDialectsForSdy(registry); }); | ||
|
||
TranslateToMLIRRegistration deserializeRegistration( | ||
"deserialize", "Deserialize a portable artifact into a SDY program", | ||
[](llvm::StringRef input, mlir::MLIRContext *context) { | ||
context->loadDialect<sdy::SdyDialect>(); | ||
auto module = parseSourceString<ModuleOp>(input, context); | ||
return module; | ||
}, | ||
[](DialectRegistry ®istry) { registerDialectsForSdy(registry); }); | ||
|
||
} // namespace mlir | ||
|
||
int main(int argc, char **argv) { | ||
return mlir::asMainReturnCode( | ||
mlir::mlirTranslateMain(argc, argv, "SDY transformation driver\n")); | ||
} |