Skip to content

Commit aa69bae

Browse files
author
Farid Zakaria
authored
Add examples directory (#2083)
Add the beginnings of an `examples` directory structure that will help demonstrate how to use the StableHLO library. At the moment I've only added a c++ example, and a simple one at that but the directory structure should indicate that we can support Python in the future. * Added a meta library 'stablehlo_lib' that you can link against which should eventually include everything * Added the add stablehlo example * Added a simple README ```console ❯ ./build/examples/c++/stablehlo-add module @test_module { func.func @main(%arg0: tensor<3x4xf32>, %arg1: tensor<3x4xf32>) -> tensor<3x4xf32> { %0 = stablehlo.add %arg0, %arg1 : tensor<3x4xf32> %1 = stablehlo.add %arg0, %arg1 : tensor<3x4xf32> return %1 : tensor<3x4xf32> } } ```
1 parent 1d61de4 commit aa69bae

File tree

6 files changed

+167
-0
lines changed

6 files changed

+167
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,4 @@ add_custom_target(check-stablehlo-quick)
187187
add_dependencies(check-stablehlo-ci check-stablehlo-quick)
188188

189189
add_subdirectory(stablehlo)
190+
add_subdirectory(examples)

examples/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2024 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
add_subdirectory(c++)

examples/c++/BUILD.bazel

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2023 The StableHLO Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
15+
16+
package(
17+
default_visibility = ["//visibility:public"],
18+
licenses = ["notice"],
19+
)
20+
21+
cc_binary(
22+
name = "stablehlo-add",
23+
srcs = [
24+
"stablehlo_add.cpp",
25+
],
26+
deps = [
27+
"//:stablehlo_ops",
28+
"@llvm-project//mlir:FuncDialect",
29+
"@llvm-project//mlir:IR",
30+
"@llvm-project//mlir:QuantOps",
31+
],
32+
)
33+
34+
cc_test(
35+
name = "stablehlo-add_test",
36+
srcs = [
37+
"stablehlo_add.cpp",
38+
],
39+
deps = [
40+
"//:stablehlo_ops",
41+
"@llvm-project//mlir:FuncDialect",
42+
"@llvm-project//mlir:IR",
43+
"@llvm-project//mlir:QuantOps",
44+
],
45+
)

examples/c++/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2024 The StableHLO Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# TODO(fzakaria): As we think about adding more tests we should consider
16+
# making this a function to be DRY.
17+
add_executable(stablehlo-add stablehlo_add.cpp)
18+
llvm_update_compile_flags(stablehlo-add)
19+
target_link_libraries(stablehlo-add PRIVATE StablehloOps)
20+
21+
mlir_check_all_link_libraries(stablehlo-add)
22+
add_custom_target(stablehlo-add-test
23+
COMMAND stablehlo-add
24+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
25+
COMMENT "Executing stablehlo-add to validate it works."
26+
DEPENDS
27+
stablehlo-add
28+
)
29+
add_dependencies(check-stablehlo-quick stablehlo-add-test)

examples/c++/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Examples
2+
3+
This directory should contain a series of examples as a starting point
4+
for how to use StableHLO.
5+
6+
Note: If you have a great example to highlight, we welcome contributions!
7+
8+
* [stablehlo_add](./stablehlo_add.cpp): A simple example that demonstrates
9+
how to use the StableHLO library to add two numbers.

examples/c++/stablehlo_add.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright 2022 The StableHLO Authors.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#include "mlir/Dialect/Func/IR/FuncOps.h"
16+
#include "mlir/Dialect/Quant/QuantOps.h"
17+
#include "mlir/IR/BuiltinOps.h"
18+
#include "mlir/IR/BuiltinTypes.h"
19+
#include "mlir/IR/Verifier.h"
20+
#include "stablehlo/dialect/StablehloOps.h"
21+
22+
int main() {
23+
mlir::MLIRContext context;
24+
25+
/** create module **/
26+
mlir::OwningOpRef<mlir::ModuleOp> module =
27+
mlir::ModuleOp::create(mlir::UnknownLoc::get(&context));
28+
module->getContext()->loadDialect<mlir::func::FuncDialect>();
29+
module->getContext()->loadDialect<mlir::stablehlo::StablehloDialect>();
30+
module->getContext()->loadDialect<mlir::quant::QuantizationDialect>();
31+
module->setName("test_module");
32+
33+
/** create function **/
34+
// create function argument and result types
35+
mlir::Type arg0 =
36+
mlir::RankedTensorType::get({3, 4}, mlir::FloatType::getF32(&context));
37+
auto func_type = mlir::FunctionType::get(&context, {arg0, arg0}, {arg0});
38+
39+
// create the function and map arguments.
40+
llvm::ArrayRef<mlir::NamedAttribute> attrs;
41+
auto function = mlir::func::FuncOp::create(mlir::UnknownLoc::get(&context),
42+
"main", func_type, attrs);
43+
function.setVisibility(mlir::func::FuncOp::Visibility::Public);
44+
module->push_back(function);
45+
46+
// create function block with add operations
47+
mlir::Block* block = function.addEntryBlock();
48+
llvm::SmallVector<mlir::Value, 4> arguments(block->args_begin(),
49+
block->args_end());
50+
mlir::OpBuilder block_builder = mlir::OpBuilder::atBlockEnd(block);
51+
mlir::Location loc = block_builder.getUnknownLoc();
52+
53+
llvm::SmallVector<mlir::NamedAttribute, 10> attributes;
54+
block_builder.create<mlir::stablehlo::AddOp>(loc, arguments, attributes)
55+
.getOperation();
56+
57+
mlir::Operation* op =
58+
block_builder
59+
.create<mlir::stablehlo::AddOp>(loc, arg0, arguments, attributes)
60+
.getOperation();
61+
block_builder.create<mlir::func::ReturnOp>(loc, op->getResult(0));
62+
63+
// verify the module and dump
64+
assert(mlir::succeeded(mlir::verify(module.get())));
65+
module->dump();
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)