Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[circle-mlir/import] Introduce import with operator helpers #14736

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions circle-mlir/circle-mlir/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ add_subdirectory(arser)
add_subdirectory(schema)
add_subdirectory(dialect)
add_subdirectory(utils)
add_subdirectory(import)
add_subdirectory(export)
12 changes: 12 additions & 0 deletions circle-mlir/circle-mlir/lib/import/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
set(SRC
src/CircleOperator.cpp
)

add_library(cirmlir_import STATIC ${SRC})
cir_mlir_static_flags(cirmlir_import)
target_include_directories(cirmlir_import PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_include_directories(cirmlir_import PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(cirmlir_import PUBLIC cirmlir_dialect)
target_link_libraries(cirmlir_import PUBLIC cirmlir_utils)
target_link_libraries(cirmlir_import PUBLIC circle_schema)
target_link_libraries(cirmlir_import PUBLIC cirmlir_coverage)
95 changes: 95 additions & 0 deletions circle-mlir/circle-mlir/lib/import/src/CircleOperator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
*
* 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.
*/

// from tensorflow/compiler/mlir/lite/flatbuffer_operator.cc

#include "CircleOperator.h"

#include <circle-mlir/dialect/CircleDialect.h>

#include <llvm/ADT/Twine.h>
#include <llvm/ADT/StringRef.h>

#include <algorithm>
#include <cassert>

namespace circle
{

// from tensorflow/lite/schema/schema_utils.cc

BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code)
{
// Caller should guarantee that the given argument value is not a nullptr.
assert(op_code != nullptr);

if (op_code->builtin_code >= 0)
return std::max(op_code->builtin_code,
static_cast<BuiltinOperator>(op_code->deprecated_builtin_code));
// circle extended
return op_code->builtin_code;
}

} // namespace circle

namespace mlir
{

bool IsStablehloOp(const circle::OperatorCodeT &op_code)
{
// NOTE As StableHLO is not treated yet, we can just return false
llvm::StringRef op_name(circle::EnumNameBuiltinOperator(circle::GetBuiltinCode(&op_code)));
return op_name.starts_with("STABLEHLO_");
}

std::string GetMlirOpNameFromOpCode(const circle::OperatorCodeT &op_code)
{
auto builtin_code = circle::GetBuiltinCode(&op_code);
if (builtin_code == circle::BuiltinOperator_IF)
{
// TODO handle 'if' when approached here
assert(false);
return std::string("Circle.If");
}

llvm::StringRef op_name(circle::EnumNameBuiltinOperator(builtin_code));

// If the Op name contains stablehlo
if (IsStablehloOp(op_code))
{
// TODO handle 'if' when approached here
assert(false);
return llvm::Twine("stablehlo.", op_name.drop_front(10).lower()).str();
}
return llvm::Twine("Circle.", op_name.lower()).str();
}

bool CustomOptionsToAttributes(const std::string &custom_code,
const std::vector<uint8_t> &custom_options, mlir::Builder builder,
mlir::Location loc,
llvm::SmallVectorImpl<mlir::NamedAttribute> *attributes)
{
attributes->emplace_back(builder.getNamedAttr("custom_code", builder.getStringAttr(custom_code)));
std::string content;
content.assign(reinterpret_cast<const char *>(custom_options.data()), custom_options.size());
attributes->emplace_back(builder.getNamedAttr(
"custom_option", mlir::Circle::ConstBytesAttr::get(builder.getContext(), content)));

return true;
}

} // namespace mlir
69 changes: 69 additions & 0 deletions circle-mlir/circle-mlir/lib/import/src/CircleOperator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
*
* 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.
*/

// from tensorflow/compiler/mlir/lite/flatbuffer_operator.h

#ifndef __CIRCLE_MLIR_IMPORT_CIRCLE_OPERATOR_H__
#define __CIRCLE_MLIR_IMPORT_CIRCLE_OPERATOR_H__

#include <circle_schema/schema_generated.h>

#include <llvm/ADT/StringRef.h>
#include <llvm/Analysis/AssumeBundleQueries.h> // MinMax
#include <mlir/IR/Builders.h>

#include <string>
#include <vector>

namespace circle
{

// from tensorflow/lite/schema/schema_utils.h

BuiltinOperator GetBuiltinCode(const OperatorCode *op_code);
BuiltinOperator GetBuiltinCode(const OperatorCodeT *op_code);

} // namespace circle

namespace mlir
{

bool IsStablehloOp(const circle::OperatorCodeT &op_code);

// Returns the MLIR op name for the flatbuffer operator corresponding to `op_code`.
std::string GetMlirOpNameFromOpCode(const circle::OperatorCodeT &op_code);

// Populates the array of mlir::NamedAttributes corresponding to the given
// circle::FlatbufferOptionsUnion.
// We use an out parameter per LLVM convention
void BuiltinOptionsToAttributes(circle::BuiltinOptionsUnion op_union, mlir::Builder builder,
// NOLINTNEXTLINE
llvm::SmallVectorImpl<mlir::NamedAttribute> &attributes);

// Populates the `custom_code` and `custom_options` to attributes.
// `custom_code` is used to identify CustomOp.
// `custom_options` are opaque attribute used to store infomations for this
// custom op.
bool CustomOptionsToAttributes(const std::string &custom_code,
const std::vector<uint8_t> &custom_options, mlir::Builder builder,
// NOLINTNEXTLINE
Location loc,
llvm::SmallVectorImpl<mlir::NamedAttribute> *attributes);

} // namespace mlir

#endif // __CIRCLE_MLIR_IMPORT_CIRCLE_OPERATOR_H__
Loading