Skip to content

Commit

Permalink
Added CAPIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sdasgup3 committed Jun 25, 2024
1 parent 5180c6a commit e85e716
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -841,12 +841,14 @@ gentbl_cc_library(
STABLEHLO_CAPI_SOURCES = [
"stablehlo/integrations/c/StablehloAttributes.cpp",
"stablehlo/integrations/c/StablehloDialect.cpp",
"stablehlo/integrations/c/StablehloPasses.cpp",
"stablehlo/integrations/c/StablehloTypes.cpp",
]

STABLEHLO_CAPI_HEADERS = [
"stablehlo/integrations/c/StablehloAttributes.h",
"stablehlo/integrations/c/StablehloDialect.h",
"stablehlo/integrations/c/StablehloPasses.h",
"stablehlo/integrations/c/StablehloTypes.h",
]

Expand All @@ -857,6 +859,7 @@ cc_library(
strip_include_prefix = ".",
deps = [
":stablehlo_ops",
":stablehlo_passes",
"@llvm-project//mlir:CAPIIR",
],
)
Expand All @@ -879,6 +882,7 @@ cc_library(
strip_include_prefix = ".",
deps = [
":stablehlo_ops",
":stablehlo_passes",
"@llvm-project//mlir:CAPIIRObjects",
],
alwayslink = True,
Expand Down
2 changes: 2 additions & 0 deletions stablehlo/integrations/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ add_mlir_public_c_api_library(StablehloCAPI
PARTIAL_SOURCES_INTENDED
StablehloAttributes.cpp
StablehloDialect.cpp
StablehloPasses.cpp
StablehloTypes.cpp

LINK_LIBS PUBLIC
StablehloOps
StablehloPasses
)

add_mlir_public_c_api_library(VhloCAPI
Expand Down
17 changes: 17 additions & 0 deletions stablehlo/integrations/c/StablehloPasses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Copyright 2024 The StableHLO 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.
==============================================================================*/

#include "stablehlo/integrations/c/StablehloPasses.h"

#include "stablehlo/transforms/Passes.h"

void mlirRegisterAllStablehloPasses() { mlir::stablehlo::registerPasses(); }
30 changes: 30 additions & 0 deletions stablehlo/integrations/c/StablehloPasses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright 2024 The StableHLO 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.
==============================================================================*/

#ifndef STABLEHLO_INTEGRATIONS_C_STABLEHLO_PASSES_H
#define STABLEHLO_INTEGRATIONS_C_STABLEHLO_PASSES_H

#include "mlir-c/IR.h"
#include "mlir-c/Support.h"

#ifdef __cplusplus
extern "C" {
#endif

/// Register all compiler passes of StableHLO.
MLIR_CAPI_EXPORTED void mlirRegisterAllStablehloPasses();

#ifdef __cplusplus
}
#endif

#endif // STABLEHLO_INTEGRATIONS_C_STABLEHLO_PASSES_H
5 changes: 3 additions & 2 deletions stablehlo/integrations/python/StablehloModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ limitations under the License.
#include "stablehlo/dialect/Serialization.h"
#include "stablehlo/integrations/c/StablehloAttributes.h"
#include "stablehlo/integrations/c/StablehloDialect.h"
#include "stablehlo/integrations/c/StablehloPasses.h"
#include "stablehlo/integrations/c/StablehloTypes.h"
#include "stablehlo/integrations/python/PortableApi.h"
#include "stablehlo/reference/Api.h"
#include "stablehlo/transforms/Passes.h"

namespace py = pybind11;

Expand Down Expand Up @@ -70,7 +70,8 @@ PYBIND11_MODULE(_stablehlo, m) {
// Passes.
//

m.def("register_passes", []() { mlir::stablehlo::registerPasses(); });
m.def("register_stablehlo_passes",
[]() { mlirRegisterAllStablehloPasses(); });

//
// Types.
Expand Down
6 changes: 3 additions & 3 deletions stablehlo/integrations/python/tests/stablehlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ def test_register_passes():
module = ir.Module.parse(ASM_FORMAT.format("2xf32"))
assert module is not None

stablehlo.register_passes()
stablehlo.register_stablehlo_passes()
pipeline = [
'stablehlo-legalize-to-vhlo',
'vhlo-legalize-to-stablehlo',
"stablehlo-legalize-to-vhlo",
"vhlo-legalize-to-stablehlo",
]
pipeline = pm.PassManager.parse(f"builtin.module({','.join(pipeline)})")

Expand Down

0 comments on commit e85e716

Please sign in to comment.