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

redo ArrayRefOrSmallVector with enable_if #1758

Merged
Merged
Changes from 2 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
28 changes: 21 additions & 7 deletions src/Builder/FrontendDialectHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ template <typename T>
using DenseElementsAttrBuilder =
llvm::function_ref<mlir::DenseElementsAttr(llvm::ArrayRef<T>)>;

// When the protobuf repeated field has a type of the same size as T,
// access the data directly via ArrayRef.
template <typename T, typename U>
std::enable_if_t<std::is_same_v<T, U>, mlir::DenseElementsAttr>
createDenseElmAttrFromProtoData(const google::protobuf::RepeatedField<U> &data,
DenseElementsAttrBuilder<T> denseBuilder) {
return denseBuilder(llvm::makeArrayRef(data.data(), data.size()));
}

// When the protobuf repeated field has a type larger than T,
// copy the data into correctly typed SmallVector because
// DenseElementsAttr needs argument type of the correct bitwidth.
template <typename T, typename U>
std::enable_if_t<!std::is_same_v<T, U>, mlir::DenseElementsAttr>
createDenseElmAttrFromProtoData(const google::protobuf::RepeatedField<U> &data,
DenseElementsAttrBuilder<T> denseBuilder) {
llvm::SmallVector<T> copy(data.begin(), data.end());
return denseBuilder(llvm::makeArrayRef(copy));
}

// Returns DenseElementsAttr with tp's data.
template <typename T>
mlir::DenseElementsAttr createDenseElmAttr(const std::string &externalDataDir,
Expand Down Expand Up @@ -148,13 +168,7 @@ mlir::DenseElementsAttr createDenseElmAttr(const std::string &externalDataDir,
} else {
// Not raw, no need to take care of endianness.
const auto &data = TransformValueToONNXData<T>::data(tp);
// Access data directly via ArrayRef if same size as T,
// or copy into correctly typed SmallVector otherwise
// because DenseElementsAttr needs argument type of the correct bitwidth.
typedef typename std::conditional<sizeof(T) == sizeof(data[0]),
llvm::ArrayRef<T>, llvm::SmallVector<T>>::type ArrayRefOrSmallVector;
ArrayRefOrSmallVector array(data.begin(), data.end());
return denseBuilder(llvm::makeArrayRef(array));
return createDenseElmAttrFromProtoData(data, denseBuilder);
}
}

Expand Down