18
18
#include " mlir/Dialect/Utils/IndexingUtils.h"
19
19
#include " mlir/IR/BuiltinAttributes.h"
20
20
#include " mlir/IR/BuiltinTypes.h"
21
+ #include " mlir/IR/DialectResourceBlobManager.h"
21
22
#include " mlir/IR/Matchers.h"
22
23
#include " mlir/Pass/Pass.h"
23
24
#include " llvm/ADT/APFloat.h"
@@ -176,13 +177,38 @@ DenseElementsAttr transposeType(const RangeType &data, ShapedType inputType,
176
177
llvm::ArrayRef<ElementType>(outputValues));
177
178
}
178
179
180
+ // Function that tries to wrap the DenseResourceElementsAttr data access
181
+ // handling as unfortunately at the moment don't share the same interface
182
+ // with DenseElementsAttr
183
+ template <typename T>
184
+ std::optional<ArrayRef<T>> tryGetDenseResourceValues (ElementsAttr attr) {
185
+ if (auto denseResource = dyn_cast<DenseResourceElementsAttr>(attr)) {
186
+ // Check that the resource memory blob exists
187
+ AsmResourceBlob *blob = denseResource.getRawHandle ().getBlob ();
188
+ if (!blob)
189
+ return std::nullopt;
190
+
191
+ // Check that the data are in a valid form
192
+ bool isSplat = false ;
193
+ if (!DenseElementsAttr::isValidRawBuffer (attr.getShapedType (),
194
+ blob->getData (), isSplat)) {
195
+ return std::nullopt;
196
+ }
197
+
198
+ return blob->template getDataAs <T>();
199
+ }
200
+
201
+ return std::nullopt;
202
+ }
203
+
179
204
// A type specialized transposition of an ElementsAttr.
180
205
// This implementation tries to operate on the underlying data in its raw
181
206
// representation when possible to avoid allocating a large number of Attribute
182
207
// objects.
183
208
DenseElementsAttr transpose (ElementsAttr attr, ShapedType inputType,
184
209
ShapedType outputType,
185
210
llvm::ArrayRef<int64_t > permValues) {
211
+ // Handle generic ElementsAttr
186
212
if (auto data = attr.tryGetValues <bool >())
187
213
return transposeType (*data, inputType, outputType, permValues);
188
214
@@ -204,6 +230,35 @@ DenseElementsAttr transpose(ElementsAttr attr, ShapedType inputType,
204
230
if (auto data = attr.tryGetValues <APFloat>())
205
231
return transposeType (*data, inputType, outputType, permValues);
206
232
233
+ // Handle DenseResourceElementsAttr
234
+ if (isa<DenseResourceElementsAttr>(attr)) {
235
+ auto elementTy = attr.getElementType ();
236
+
237
+ if (auto data = tryGetDenseResourceValues<bool >(attr);
238
+ data && elementTy.isInteger (1 ))
239
+ return transposeType (*data, inputType, outputType, permValues);
240
+
241
+ if (auto data = tryGetDenseResourceValues<int8_t >(attr);
242
+ data && elementTy.isInteger (8 ))
243
+ return transposeType (*data, inputType, outputType, permValues);
244
+
245
+ if (auto data = tryGetDenseResourceValues<int16_t >(attr);
246
+ data && elementTy.isInteger (16 ))
247
+ return transposeType (*data, inputType, outputType, permValues);
248
+
249
+ if (auto data = tryGetDenseResourceValues<int32_t >(attr);
250
+ data && elementTy.isInteger (32 ))
251
+ return transposeType (*data, inputType, outputType, permValues);
252
+
253
+ if (auto data = tryGetDenseResourceValues<int64_t >(attr);
254
+ data && elementTy.isInteger (64 ))
255
+ return transposeType (*data, inputType, outputType, permValues);
256
+
257
+ if (auto data = tryGetDenseResourceValues<float >(attr);
258
+ data && elementTy.isF32 ())
259
+ return transposeType (*data, inputType, outputType, permValues);
260
+ }
261
+
207
262
return nullptr ;
208
263
}
209
264
0 commit comments