|
| 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