@@ -219,76 +219,78 @@ class TypeConverter {
219
219
// / conversion exists, success otherwise. If the new set of types is empty,
220
220
// / the type is removed and any usages of the existing value are expected to
221
221
// / be removed during conversion.
222
- LogicalResult convertType (Type t, SmallVectorImpl<Type> &results);
222
+ LogicalResult convertType (Type t, SmallVectorImpl<Type> &results) const ;
223
223
224
224
// / This hook simplifies defining 1-1 type conversions. This function returns
225
225
// / the type to convert to on success, and a null type on failure.
226
- Type convertType (Type t);
226
+ Type convertType (Type t) const ;
227
227
228
228
// / Attempts a 1-1 type conversion, expecting the result type to be
229
229
// / `TargetType`. Returns the converted type cast to `TargetType` on success,
230
230
// / and a null type on conversion or cast failure.
231
- template <typename TargetType>
232
- TargetType convertType (Type t) {
231
+ template <typename TargetType> TargetType convertType (Type t) const {
233
232
return dyn_cast_or_null<TargetType>(convertType (t));
234
233
}
235
234
236
235
// / Convert the given set of types, filling 'results' as necessary. This
237
236
// / returns failure if the conversion of any of the types fails, success
238
237
// / otherwise.
239
- LogicalResult convertTypes (TypeRange types, SmallVectorImpl<Type> &results);
238
+ LogicalResult convertTypes (TypeRange types,
239
+ SmallVectorImpl<Type> &results) const ;
240
240
241
241
// / Return true if the given type is legal for this type converter, i.e. the
242
242
// / type converts to itself.
243
- bool isLegal (Type type);
243
+ bool isLegal (Type type) const ;
244
+
244
245
// / Return true if all of the given types are legal for this type converter.
245
246
template <typename RangeT>
246
247
std::enable_if_t <!std::is_convertible<RangeT, Type>::value &&
247
248
!std::is_convertible<RangeT, Operation *>::value,
248
249
bool >
249
- isLegal (RangeT &&range) {
250
+ isLegal (RangeT &&range) const {
250
251
return llvm::all_of (range, [this ](Type type) { return isLegal (type); });
251
252
}
252
253
// / Return true if the given operation has legal operand and result types.
253
- bool isLegal (Operation *op);
254
+ bool isLegal (Operation *op) const ;
254
255
255
256
// / Return true if the types of block arguments within the region are legal.
256
- bool isLegal (Region *region);
257
+ bool isLegal (Region *region) const ;
257
258
258
259
// / Return true if the inputs and outputs of the given function type are
259
260
// / legal.
260
- bool isSignatureLegal (FunctionType ty);
261
+ bool isSignatureLegal (FunctionType ty) const ;
261
262
262
263
// / This method allows for converting a specific argument of a signature. It
263
264
// / takes as inputs the original argument input number, type.
264
265
// / On success, it populates 'result' with any new mappings.
265
266
LogicalResult convertSignatureArg (unsigned inputNo, Type type,
266
- SignatureConversion &result);
267
+ SignatureConversion &result) const ;
267
268
LogicalResult convertSignatureArgs (TypeRange types,
268
269
SignatureConversion &result,
269
- unsigned origInputOffset = 0 );
270
+ unsigned origInputOffset = 0 ) const ;
270
271
271
272
// / This function converts the type signature of the given block, by invoking
272
273
// / 'convertSignatureArg' for each argument. This function should return a
273
274
// / valid conversion for the signature on success, std::nullopt otherwise.
274
- std::optional<SignatureConversion> convertBlockSignature (Block *block);
275
+ std::optional<SignatureConversion> convertBlockSignature (Block *block) const ;
275
276
276
277
// / Materialize a conversion from a set of types into one result type by
277
278
// / generating a cast sequence of some kind. See the respective
278
279
// / `add*Materialization` for more information on the context for these
279
280
// / methods.
280
281
Value materializeArgumentConversion (OpBuilder &builder, Location loc,
281
- Type resultType, ValueRange inputs) {
282
+ Type resultType,
283
+ ValueRange inputs) const {
282
284
return materializeConversion (argumentMaterializations, builder, loc,
283
285
resultType, inputs);
284
286
}
285
287
Value materializeSourceConversion (OpBuilder &builder, Location loc,
286
- Type resultType, ValueRange inputs) {
288
+ Type resultType, ValueRange inputs) const {
287
289
return materializeConversion (sourceMaterializations, builder, loc,
288
290
resultType, inputs);
289
291
}
290
292
Value materializeTargetConversion (OpBuilder &builder, Location loc,
291
- Type resultType, ValueRange inputs) {
293
+ Type resultType, ValueRange inputs) const {
292
294
return materializeConversion (targetMaterializations, builder, loc,
293
295
resultType, inputs);
294
296
}
@@ -297,7 +299,8 @@ class TypeConverter {
297
299
// / the registered conversion functions. If no applicable conversion has been
298
300
// / registered, return std::nullopt. Note that the empty attribute/`nullptr`
299
301
// / is a valid return value for this function.
300
- std::optional<Attribute> convertTypeAttribute (Type type, Attribute attr);
302
+ std::optional<Attribute> convertTypeAttribute (Type type,
303
+ Attribute attr) const ;
301
304
302
305
private:
303
306
// / The signature of the callback used to convert a type. If the new set of
@@ -316,16 +319,17 @@ class TypeConverter {
316
319
317
320
// / Attempt to materialize a conversion using one of the provided
318
321
// / materialization functions.
319
- Value materializeConversion (
320
- MutableArrayRef<MaterializationCallbackFn> materializations,
321
- OpBuilder &builder, Location loc, Type resultType, ValueRange inputs);
322
+ Value
323
+ materializeConversion (ArrayRef<MaterializationCallbackFn> materializations,
324
+ OpBuilder &builder, Location loc, Type resultType,
325
+ ValueRange inputs) const ;
322
326
323
327
// / Generate a wrapper for the given callback. This allows for accepting
324
328
// / different callback forms, that all compose into a single version.
325
329
// / With callback of form: `std::optional<Type>(T)`
326
330
template <typename T, typename FnT>
327
331
std::enable_if_t <std::is_invocable_v<FnT, T>, ConversionCallbackFn>
328
- wrapCallback (FnT &&callback) {
332
+ wrapCallback (FnT &&callback) const {
329
333
return wrapCallback<T>(
330
334
[callback = std::forward<FnT>(callback)](
331
335
T type, SmallVectorImpl<Type> &results, ArrayRef<Type>) {
@@ -343,7 +347,7 @@ class TypeConverter {
343
347
template <typename T, typename FnT>
344
348
std::enable_if_t <std::is_invocable_v<FnT, T, SmallVectorImpl<Type> &>,
345
349
ConversionCallbackFn>
346
- wrapCallback (FnT &&callback) {
350
+ wrapCallback (FnT &&callback) const {
347
351
return wrapCallback<T>(
348
352
[callback = std::forward<FnT>(callback)](
349
353
T type, SmallVectorImpl<Type> &results, ArrayRef<Type>) {
@@ -356,7 +360,7 @@ class TypeConverter {
356
360
std::enable_if_t <
357
361
std::is_invocable_v<FnT, T, SmallVectorImpl<Type> &, ArrayRef<Type>>,
358
362
ConversionCallbackFn>
359
- wrapCallback (FnT &&callback) {
363
+ wrapCallback (FnT &&callback) const {
360
364
return [callback = std::forward<FnT>(callback)](
361
365
Type type, SmallVectorImpl<Type> &results,
362
366
ArrayRef<Type> callStack) -> std::optional<LogicalResult> {
@@ -378,7 +382,7 @@ class TypeConverter {
378
382
// / may take any subclass of `Type` and the wrapper will check for the target
379
383
// / type to be of the expected class before calling the callback.
380
384
template <typename T, typename FnT>
381
- MaterializationCallbackFn wrapMaterialization (FnT &&callback) {
385
+ MaterializationCallbackFn wrapMaterialization (FnT &&callback) const {
382
386
return [callback = std::forward<FnT>(callback)](
383
387
OpBuilder &builder, Type resultType, ValueRange inputs,
384
388
Location loc) -> std::optional<Value> {
@@ -394,7 +398,7 @@ class TypeConverter {
394
398
// / callback.
395
399
template <typename T, typename A, typename FnT>
396
400
TypeAttributeConversionCallbackFn
397
- wrapTypeAttributeConversion (FnT &&callback) {
401
+ wrapTypeAttributeConversion (FnT &&callback) const {
398
402
return [callback = std::forward<FnT>(callback)](
399
403
Type type, Attribute attr) -> AttributeConversionResult {
400
404
if (T derivedType = dyn_cast<T>(type)) {
@@ -428,13 +432,13 @@ class TypeConverter {
428
432
// / A set of cached conversions to avoid recomputing in the common case.
429
433
// / Direct 1-1 conversions are the most common, so this cache stores the
430
434
// / successful 1-1 conversions as well as all failed conversions.
431
- DenseMap<Type, Type> cachedDirectConversions;
435
+ mutable DenseMap<Type, Type> cachedDirectConversions;
432
436
// / This cache stores the successful 1->N conversions, where N != 1.
433
- DenseMap<Type, SmallVector<Type, 2 >> cachedMultiConversions;
437
+ mutable DenseMap<Type, SmallVector<Type, 2 >> cachedMultiConversions;
434
438
435
439
// / Stores the types that are being converted in the case when convertType
436
440
// / is being called recursively to convert nested types.
437
- SmallVector<Type, 2 > conversionCallStack;
441
+ mutable SmallVector<Type, 2 > conversionCallStack;
438
442
};
439
443
440
444
// ===----------------------------------------------------------------------===//
0 commit comments