Skip to content

Commit

Permalink
redo ArrayRefOrSmallVector with enable_if
Browse files Browse the repository at this point in the history
attempt to address issue onnx#1729: Build error on Linux

Signed-off-by: Soren Lassen <[email protected]>
  • Loading branch information
sorenlassen committed Sep 24, 2022
1 parent df104fb commit ac7aeac
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/Builder/FrontendDialectHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,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.begin(), data.end()));
}

// 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 @@ -147,13 +167,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

0 comments on commit ac7aeac

Please sign in to comment.