Skip to content

Commit

Permalink
Refactor portable api to use main registration method (openxla#1981)
Browse files Browse the repository at this point in the history
Less moving pieces. Will also get the QuantizationDialect registration
that was added earlier.

We end up registering _some_ dialects that aren't needed in
serialization, but the cost is likely not worth the duplication.
  • Loading branch information
GleasonK authored Feb 1, 2024
1 parent 550d946 commit d50b6fc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ cc_library(
],
strip_include_prefix = ".",
deps = [
":register",
":stablehlo_ops",
":stablehlo_serialization",
":version",
Expand Down
1 change: 1 addition & 0 deletions stablehlo/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_mlir_dialect_library(StablehloPortableApi
LINK_LIBS PUBLIC
ChloOps
StablehloOps
StablehloRegister
StablehloSerialization
Version
VhloOps
Expand Down
13 changes: 7 additions & 6 deletions stablehlo/api/PortableApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Parser/Parser.h"
#include "stablehlo/dialect/Register.h"
#include "stablehlo/dialect/Serialization.h"
#include "stablehlo/dialect/StablehloOps.h"
#include "stablehlo/dialect/Version.h"
Expand All @@ -29,10 +30,10 @@ limitations under the License.
namespace mlir {
namespace stablehlo {
namespace {
void loadSerializationDialects(MLIRContext* context) {
context->loadDialect<mlir::func::FuncDialect>();
context->loadDialect<mlir::stablehlo::StablehloDialect>();
context->loadDialect<mlir::vhlo::VhloDialect>();
void loadSerializationDialects(MLIRContext& context) {
mlir::DialectRegistry registry;
mlir::stablehlo::registerAllDialects(registry);
context.appendDialectRegistry(registry);
}
} // namespace

Expand All @@ -48,7 +49,7 @@ LogicalResult serializePortableArtifact(StringRef moduleStr,
StringRef targetVersion,
raw_ostream& os) {
MLIRContext context;
loadSerializationDialects(&context);
loadSerializationDialects(context);
auto module = mlir::parseSourceString<mlir::ModuleOp>(moduleStr, &context);
if (!module || failed(module->verifyInvariants())) return failure();

Expand All @@ -58,7 +59,7 @@ LogicalResult serializePortableArtifact(StringRef moduleStr,
LogicalResult deserializePortableArtifact(StringRef artifactStr,
raw_ostream& os) {
MLIRContext context;
loadSerializationDialects(&context);
loadSerializationDialects(context);
auto module = deserializePortableArtifact(artifactStr, &context);
if (!module) return failure();

Expand Down

0 comments on commit d50b6fc

Please sign in to comment.