@@ -137,10 +137,12 @@ namespace {
137
137
struct ValueVectorMapInfo {
138
138
static ValueVector getEmptyKey () { return ValueVector{}; }
139
139
static ValueVector getTombstoneKey () { return ValueVector{}; }
140
- static ::llvm::hash_code getHashValue (ValueVector val) {
140
+ static ::llvm::hash_code getHashValue (const ValueVector & val) {
141
141
return ::llvm::hash_combine_range (val.begin (), val.end ());
142
142
}
143
- static bool isEqual (ValueVector LHS, ValueVector RHS) { return LHS == RHS; }
143
+ static bool isEqual (const ValueVector &LHS, const ValueVector &RHS) {
144
+ return LHS == RHS;
145
+ }
144
146
};
145
147
146
148
// / This class wraps a IRMapping to provide recursive lookup
@@ -159,20 +161,18 @@ struct ConversionValueMapping {
159
161
// / - If there is no mapping to the desired types, also return the most
160
162
// / recently mapped values.
161
163
// / - If there is no mapping for the given values at all, return the given
162
- // / values.
163
- ValueVector lookupOrDefault (ValueVector from,
164
- TypeRange desiredTypes = {}) const ;
164
+ // / value.
165
+ ValueVector lookupOrDefault (Value from, TypeRange desiredTypes = {}) const ;
165
166
166
- // / Lookup the given values within the map, or return an empty vector if the
167
- // / values are not mapped. If they are mapped, this follows the same behavior
167
+ // / Lookup the given value within the map, or return an empty vector if the
168
+ // / value is not mapped. If it is mapped, this follows the same behavior
168
169
// / as `lookupOrDefault`.
169
- ValueVector lookupOrNull (const ValueVector &from,
170
- TypeRange desiredTypes = {}) const ;
170
+ ValueVector lookupOrNull (Value from, TypeRange desiredTypes = {}) const ;
171
171
172
172
template <typename T>
173
173
struct IsValueVector : std::is_same<std::decay_t <T>, ValueVector> {};
174
174
175
- // / Map a value to the one provided.
175
+ // / Map a value vector to the one provided.
176
176
template <typename OldVal, typename NewVal>
177
177
std::enable_if_t <IsValueVector<OldVal>{} && IsValueVector<NewVal>{}>
178
178
map (OldVal &&oldVal, NewVal &&newVal) {
@@ -192,6 +192,7 @@ struct ConversionValueMapping {
192
192
mapping[std::forward<OldVal>(oldVal)] = std::forward<NewVal>(newVal);
193
193
}
194
194
195
+ // / Map a value vector or single value to the one provided.
195
196
template <typename OldVal, typename NewVal>
196
197
std::enable_if_t <!IsValueVector<OldVal>{} || !IsValueVector<NewVal>{}>
197
198
map (OldVal &&oldVal, NewVal &&newVal) {
@@ -217,53 +218,56 @@ struct ConversionValueMapping {
217
218
} // namespace
218
219
219
220
ValueVector
220
- ConversionValueMapping::lookupOrDefault (ValueVector from,
221
+ ConversionValueMapping::lookupOrDefault (Value from,
221
222
TypeRange desiredTypes) const {
222
223
// Try to find the deepest values that have the desired types. If there is no
223
224
// such mapping, simply return the deepest values.
224
225
ValueVector desiredValue;
226
+ ValueVector current{from};
225
227
do {
226
228
// Store the current value if the types match.
227
- if (desiredTypes. empty () || TypeRange (from ) == desiredTypes)
228
- desiredValue = from ;
229
+ if (TypeRange (current ) == desiredTypes)
230
+ desiredValue = current ;
229
231
230
232
// If possible, Replace each value with (one or multiple) mapped values.
231
233
ValueVector next;
232
- for (Value v : from ) {
234
+ for (Value v : current ) {
233
235
auto it = mapping.find ({v});
234
236
if (it != mapping.end ()) {
235
237
llvm::append_range (next, it->second );
236
238
} else {
237
239
next.push_back (v);
238
240
}
239
241
}
240
- if (next != from ) {
242
+ if (next != current ) {
241
243
// If at least one value was replaced, continue the lookup from there.
242
- from = std::move (next);
244
+ current = std::move (next);
243
245
continue ;
244
246
}
245
247
246
248
// Otherwise: Check if there is a mapping for the entire vector. Such
247
249
// mappings are materializations. (N:M mapping are not supported for value
248
250
// replacements.)
249
- auto it = mapping.find (from );
251
+ auto it = mapping.find (current );
250
252
if (it == mapping.end ()) {
251
253
// No mapping found: The lookup stops here.
252
254
break ;
253
255
}
254
- from = it->second ;
256
+ current = it->second ;
255
257
} while (true );
256
258
257
259
// If the desired values were found use them, otherwise default to the leaf
258
260
// values.
259
- return !desiredValue.empty () ? desiredValue : from;
261
+ // Note: If `desiredTypes` is empty, this function always returns `current`.
262
+ return !desiredValue.empty () ? desiredValue : current;
260
263
}
261
264
262
- ValueVector ConversionValueMapping::lookupOrNull (const ValueVector & from,
265
+ ValueVector ConversionValueMapping::lookupOrNull (Value from,
263
266
TypeRange desiredTypes) const {
264
267
ValueVector result = lookupOrDefault (from, desiredTypes);
265
268
TypeRange resultTypes (result);
266
- if (result == from || (!desiredTypes.empty () && resultTypes != desiredTypes))
269
+ if (result == ValueVector{from} ||
270
+ (!desiredTypes.empty () && resultTypes != desiredTypes))
267
271
return {};
268
272
return result;
269
273
}
@@ -1261,7 +1265,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
1261
1265
// The current pattern does not have a type converter. I.e., it does not
1262
1266
// distinguish between legal and illegal types. For each operand, simply
1263
1267
// pass through the most recently mapped values.
1264
- remapped.push_back (mapping.lookupOrDefault ({ operand} ));
1268
+ remapped.push_back (mapping.lookupOrDefault (operand));
1265
1269
continue ;
1266
1270
}
1267
1271
@@ -1280,7 +1284,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
1280
1284
continue ;
1281
1285
}
1282
1286
1283
- ValueVector repl = mapping.lookupOrDefault ({ operand} , legalTypes);
1287
+ ValueVector repl = mapping.lookupOrDefault (operand, legalTypes);
1284
1288
if (!repl.empty () && TypeRange (repl) == legalTypes) {
1285
1289
// Mapped values have the correct type or there is an existing
1286
1290
// materialization. Or the operand is not mapped at all and has the
@@ -1290,7 +1294,7 @@ LogicalResult ConversionPatternRewriterImpl::remapValues(
1290
1294
}
1291
1295
1292
1296
// Create a materialization for the most recently mapped values.
1293
- repl = mapping.lookupOrDefault ({ operand} );
1297
+ repl = mapping.lookupOrDefault (operand);
1294
1298
ValueRange castValues = buildUnresolvedMaterialization (
1295
1299
MaterializationKind::Target, computeInsertPoint (repl), operandLoc,
1296
1300
/* valuesToMap=*/ repl, /* inputs=*/ repl, /* outputTypes=*/ legalTypes,
@@ -1428,10 +1432,7 @@ Block *ConversionPatternRewriterImpl::applySignatureConversion(
1428
1432
continue ;
1429
1433
}
1430
1434
1431
- // This is a 1->1+ mapping. 1->N mappings are not fully supported in the
1432
- // dialect conversion. Therefore, we need an argument materialization to
1433
- // turn the replacement block arguments into a single SSA value that can be
1434
- // used as a replacement.
1435
+ // This is a 1->1+ mapping.
1435
1436
auto replArgs =
1436
1437
newBlock->getArguments ().slice (inputMap->inputNo , inputMap->size );
1437
1438
ValueVector replArgVals = llvm::to_vector_of<Value, 1 >(replArgs);
@@ -1487,7 +1488,7 @@ ValueRange ConversionPatternRewriterImpl::buildUnresolvedMaterialization(
1487
1488
Value ConversionPatternRewriterImpl::findOrBuildReplacementValue (
1488
1489
Value value, const TypeConverter *converter) {
1489
1490
// Find a replacement value with the same type.
1490
- ValueVector repl = mapping.lookupOrNull ({ value} , value.getType ());
1491
+ ValueVector repl = mapping.lookupOrNull (value, value.getType ());
1491
1492
if (!repl.empty ())
1492
1493
return repl.front ();
1493
1494
@@ -1503,7 +1504,7 @@ Value ConversionPatternRewriterImpl::findOrBuildReplacementValue(
1503
1504
// No replacement value was found. Get the latest replacement value
1504
1505
// (regardless of the type) and build a source materialization to the
1505
1506
// original type.
1506
- repl = mapping.lookupOrNull ({ value} );
1507
+ repl = mapping.lookupOrNull (value);
1507
1508
if (repl.empty ()) {
1508
1509
// No replacement value is registered in the mapping. This means that the
1509
1510
// value is dropped and no longer needed. (If the value were still needed,
@@ -1742,7 +1743,7 @@ void ConversionPatternRewriter::replaceUsesOfBlockArgument(BlockArgument from,
1742
1743
});
1743
1744
impl->appendRewrite <ReplaceBlockArgRewrite>(from.getOwner (), from,
1744
1745
impl->currentTypeConverter );
1745
- impl->mapping .map (impl->mapping .lookupOrDefault ({ from} ), to);
1746
+ impl->mapping .map (impl->mapping .lookupOrDefault (from), to);
1746
1747
}
1747
1748
1748
1749
Value ConversionPatternRewriter::getRemappedValue (Value key) {
0 commit comments