46#include <mlir/IR/BuiltinOps.h>
47#include <mlir/IR/PatternMatch.h>
49#include <llvm/ADT/DenseMap.h>
50#include <llvm/ADT/DenseSet.h>
51#include <llvm/ADT/SmallVector.h>
52#include <llvm/ADT/StringMap.h>
53#include <llvm/Support/raw_ostream.h>
60#define GEN_PASS_DEF_TYPEVARINFERENCEPASS
66#define DEBUG_TYPE "llzk-infer-tvar"
97using SpecializedCallableCloneCache = DenseMap<Operation *, llvm::StringMap<SymbolRefAttr>>;
98using SpecializedTemplateCloneCache = DenseMap<Operation *, llvm::StringMap<StringAttr>>;
105static SmallVector<StringAttr> getStringPieces(SymbolRefAttr ref) {
106 return llvm::to_vector(llvm::map_range(
getPieces(ref), [](FlatSymbolRefAttr piece) {
107 return piece.getAttr();
112static StringAttr getFlatSymbolName(Attribute attr) {
113 auto symRef = llvm::dyn_cast_if_present<SymbolRefAttr>(attr);
114 if (!symRef || !symRef.getNestedReferences().empty()) {
117 return symRef.getRootReference();
128 std::optional<Type> declaredTy = op.
getTypeOpt();
132 auto tvarTy = llvm::dyn_cast<TypeVarType>(*declaredTy);
133 return tvarTy && tvarTy.getRefName() == op.getName();
142static bool isCurrentTemplateTypeVarParamSymbol(StringAttr symbolName, Operation *op) {
148 return paramOp && isTypeVarParam(paramOp);
152template <
typename ConverterT>
154convertTypeRange(TypeRange oldTypes, SmallVectorImpl<Type> &newTypes, ConverterT &converter) {
155 bool changed =
false;
156 newTypes.reserve(oldTypes.size());
157 for (Type oldTy : oldTypes) {
158 Type newTy = converter.convertType(oldTy);
159 newTypes.push_back(newTy);
160 changed |= newTy != oldTy;
166template <
typename ConverterT>
167static Type convertPodType(
PodType podTy, MLIRContext *ctx, ConverterT &converter) {
168 SmallVector<RecordAttr> newRecords;
169 bool changed =
false;
170 for (RecordAttr record : podTy.
getRecords()) {
171 Type newRecordTy = converter.convertType(record.getType());
172 newRecords.push_back(RecordAttr::get(ctx, record.getName(), newRecordTy));
173 changed |= newRecordTy != record.getType();
179template <
typename ConvertElementFn>
180static Type convertArrayElementType(
ArrayType arrTy, ConvertElementFn convertElement) {
189template <
typename ConverterT>
190static Type convertFunctionType(FunctionType funcTy, ConverterT &converter) {
191 SmallVector<Type> newInputs;
192 SmallVector<Type> newResults;
193 bool changed = convertTypeRange(funcTy.getInputs(), newInputs, converter);
194 changed |= convertTypeRange(funcTy.getResults(), newResults, converter);
195 return changed ? FunctionType::get(funcTy.getContext(), newInputs, newResults) : funcTy;
199template <
typename ConvertTypeFn,
typename ConvertNestedAttrFn>
200static Attribute convertTypeOrArrayAttr(
201 Attribute attr, MLIRContext *ctx, ConvertTypeFn convertType,
202 ConvertNestedAttrFn convertNestedAttr
207 if (
auto tyAttr = llvm::dyn_cast<TypeAttr>(attr)) {
208 Type newTy = convertType(tyAttr.getValue());
209 return newTy == tyAttr.getValue() ? attr : TypeAttr::get(newTy);
211 if (
auto arrAttr = llvm::dyn_cast<ArrayAttr>(attr)) {
212 SmallVector<Attribute> newAttrs;
213 bool changed =
false;
214 for (Attribute nested : arrAttr.getValue()) {
215 Attribute newNested = convertNestedAttr(nested);
216 newAttrs.push_back(newNested);
217 changed |= newNested != nested;
219 return changed ? ArrayAttr::get(ctx, newAttrs) : attr;
226static bool templateArgUnifiesWithType(Attribute attr, Type expectedTy) {
227 Attribute expectedAttr = TypeAttr::get(expectedTy);
228 return typeParamsUnify(ArrayRef<Attribute> {attr}, ArrayRef<Attribute> {expectedAttr});
238class TypeVarReplacementConverter {
242 SmallVector<StringAttr> templatePath_;
244 SmallVector<StringAttr> oldParamOrder_;
246 DenseSet<StringAttr> removedParams_;
248 DenseMap<StringAttr, Type> replacements_;
250 bool trimResolvedParams_;
258 TypeVarReplacementConverter(
259 MLIRContext *c, ArrayRef<StringAttr> templatePath, ArrayRef<StringAttr> oldParamOrder,
260 const DenseMap<StringAttr, Type> &replacements,
bool trimResolvedParams =
true
262 : ctx_(c), templatePath_(templatePath), oldParamOrder_(oldParamOrder),
263 replacements_(replacements), trimResolvedParams_(trimResolvedParams) {
264 for (
const auto &entry : replacements_) {
265 removedParams_.insert(entry.first);
273 Type convertType(Type ty)
const {
274 DenseSet<StringAttr> resolvingParams;
275 return convertType(ty, resolvingParams);
280 Type convertType(Type ty, DenseSet<StringAttr> &resolvingParams)
const {
284 if (
auto tvarTy = llvm::dyn_cast<TypeVarType>(ty)) {
285 StringAttr paramName = tvarTy.getNameRef().getAttr();
286 auto it = replacements_.find(paramName);
287 if (it == replacements_.end()) {
290 if (!resolvingParams.insert(paramName).second) {
293 Type replacement = convertType(it->second, resolvingParams);
294 resolvingParams.erase(paramName);
297 if (
auto arrTy = llvm::dyn_cast<ArrayType>(ty)) {
298 return convertArrayElementType(arrTy, [
this, &resolvingParams](Type elemTy) {
299 return convertType(elemTy, resolvingParams);
302 if (
auto structTy = llvm::dyn_cast<StructType>(ty)) {
303 return convertStructType(structTy, resolvingParams);
305 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
306 return convertPodType(podTy, ctx_, *
this);
308 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
309 return convertFunctionType(funcTy, *
this);
320 Attribute convertAttr(Attribute attr)
const {
321 DenseSet<StringAttr> resolvingParams;
322 return convertAttr(attr, resolvingParams);
327 LogicalResult validateOperation(Operation *op)
const {
328 if (
auto createOp = llvm::dyn_cast<CreateArrayOp>(op)) {
329 if (failed(validateCreateArrayOp(createOp))) {
333 if (
auto func = llvm::dyn_cast<FuncDefOp>(op)) {
334 if (failed(validateType(func.getFunctionType(), op))) {
338 for (Region ®ion : op->getRegions()) {
339 for (Block &block : region.getBlocks()) {
340 for (Type argTy : block.getArgumentTypes()) {
341 if (failed(validateType(argTy, op))) {
347 for (Type resultTy : op->getResultTypes()) {
348 if (failed(validateType(resultTy, op))) {
352 for (NamedAttribute attr : op->getAttrs()) {
353 if (failed(validateAttr(attr.getValue(), op))) {
369 LogicalResult validateCreateArrayOp(CreateArrayOp createOp)
const {
374 ArrayType oldResultTy = createOp.getType();
375 auto newResultTy = llvm::dyn_cast<ArrayType>(convertType(oldResultTy));
376 auto newElemTy = llvm::dyn_cast<ArrayType>(convertType(oldResultTy.
getElementType()));
377 if (!newResultTy || !newElemTy || newResultTy == oldResultTy || oldResultTy.hasStaticShape()) {
381 return createOp.emitError()
382 <<
"cannot rewrite initialized array.new with non-static initializer shape "
387 LogicalResult validateType(Type ty, Operation *diagnosticOp)
const {
391 if (
auto arrTy = llvm::dyn_cast<ArrayType>(ty)) {
394 if (
auto structTy = llvm::dyn_cast<StructType>(ty)) {
395 return validateStructType(structTy, diagnosticOp);
397 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
398 for (RecordAttr record : podTy.
getRecords()) {
399 if (failed(validateType(record.getType(), diagnosticOp))) {
405 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
406 for (Type inputTy : funcTy.getInputs()) {
407 if (failed(validateType(inputTy, diagnosticOp))) {
411 for (Type resultTy : funcTy.getResults()) {
412 if (failed(validateType(resultTy, diagnosticOp))) {
421 LogicalResult validateAttr(Attribute attr, Operation *diagnosticOp)
const {
425 if (
auto tyAttr = llvm::dyn_cast<TypeAttr>(attr)) {
426 return validateType(tyAttr.getValue(), diagnosticOp);
428 if (
auto arrAttr = llvm::dyn_cast<ArrayAttr>(attr)) {
429 for (Attribute nested : arrAttr.getValue()) {
430 if (failed(validateAttr(nested, diagnosticOp))) {
439 Attribute convertAttr(Attribute attr, DenseSet<StringAttr> &resolvingParams)
const {
440 return convertTypeOrArrayAttr(attr, ctx_, [
this, &resolvingParams](Type ty) {
441 return convertType(ty, resolvingParams);
442 }, [
this, &resolvingParams](Attribute nested) {
443 return convertTemplateArgAttr(nested, resolvingParams);
455 FailureOr<ArrayAttr> convertTemplateParams(
456 ArrayAttr params, Operation *diagnosticOp,
bool resolveTemplateSymbolArgs =
true,
457 bool allowCurrentTemplateTypeVarSymbols =
false
462 if (params.size() != oldParamOrder_.size()) {
465 SmallVector<Attribute> kept;
466 for (
auto [paramName, attr] : llvm::zip_equal(oldParamOrder_, params.getValue())) {
467 if (removedParams_.contains(paramName)) {
468 if (failed(checkRemovedTemplateParam(
469 paramName, attr, diagnosticOp, resolveTemplateSymbolArgs,
470 allowCurrentTemplateTypeVarSymbols
476 kept.push_back(resolveTemplateSymbolArgs ? convertTemplateArgAttr(attr) : attr);
478 return kept.empty() ? ArrayAttr() : ArrayAttr::get(ctx_, kept);
483 bool hasWildcardForRemovedParam(ArrayAttr params)
const {
484 if (!params || params.size() != oldParamOrder_.size()) {
487 for (
auto [paramName, attr] : llvm::zip_equal(oldParamOrder_, params.getValue())) {
488 if (!removedParams_.contains(paramName)) {
491 if (
auto intAttr = llvm::dyn_cast<IntegerAttr>(attr); intAttr &&
isDynamic(intAttr)) {
500 Attribute convertTemplateArgAttr(Attribute attr)
const {
501 DenseSet<StringAttr> resolvingParams;
502 return convertTemplateArgAttr(attr, resolvingParams);
511 Attribute convertTemplateArgAttr(Attribute attr, DenseSet<StringAttr> &resolvingParams)
const {
512 if (StringAttr symbolName = getFlatSymbolName(attr)) {
513 auto it = replacements_.find(symbolName);
514 if (it != replacements_.end()) {
515 if (!resolvingParams.insert(symbolName).second) {
518 Type replacement = convertType(it->second, resolvingParams);
519 resolvingParams.erase(symbolName);
520 return TypeAttr::get(replacement);
523 return convertAttr(attr, resolvingParams);
527 LogicalResult checkRemovedTemplateParam(
528 StringAttr paramName, Attribute attr, Operation *diagnosticOp,
bool resolveTemplateSymbolArgs,
529 bool allowCurrentTemplateTypeVarSymbols =
false
531 auto replacementIt = replacements_.find(paramName);
532 assert(replacementIt != replacements_.end() &&
"removed parameter must have a replacement");
533 if (
auto intAttr = llvm::dyn_cast<IntegerAttr>(attr); intAttr &&
isDynamic(intAttr)) {
536 Attribute convertedAttr = resolveTemplateSymbolArgs ? convertTemplateArgAttr(attr) : attr;
537 if (StringAttr symbolName = getFlatSymbolName(convertedAttr)) {
538 if (allowCurrentTemplateTypeVarSymbols &&
539 isCurrentTemplateTypeVarParamSymbol(symbolName, diagnosticOp)) {
542 return emitRemovedTemplateParamMismatch(paramName, attr, replacementIt->second, diagnosticOp);
544 if (templateArgUnifiesWithType(convertedAttr, convertType(replacementIt->second))) {
548 return emitRemovedTemplateParamMismatch(paramName, attr, replacementIt->second, diagnosticOp);
552 LogicalResult emitRemovedTemplateParamMismatch(
553 StringAttr paramName, Attribute attr, Type replacementTy, Operation *diagnosticOp
555 InFlightDiagnostic diag = diagnosticOp->emitError()
556 <<
"explicit template argument for inferred parameter @"
557 << paramName.getValue() <<
" must match inferred type "
558 << replacementTy <<
", but found ";
559 if (
auto typeAttr = llvm::dyn_cast<TypeAttr>(attr)) {
560 diag << typeAttr.getValue();
573 bool isOwnedStructType(StructType structTy)
const {
574 SmallVector<StringAttr> structPath = getStringPieces(structTy.
getNameRef());
575 return structPath.size() > templatePath_.size() &&
576 std::equal(templatePath_.begin(), templatePath_.end(), structPath.begin());
584 StructType convertStructType(StructType structTy, DenseSet<StringAttr> &resolvingParams)
const {
590 SmallVector<Attribute> newParams;
591 bool changed =
false;
592 bool removeOwnedParams = isOwnedStructType(structTy) && params.size() == oldParamOrder_.size();
593 for (
auto [index, attr] : llvm::enumerate(params.getValue())) {
594 if (trimResolvedParams_ && removeOwnedParams &&
595 removedParams_.contains(oldParamOrder_[index])) {
599 Attribute newAttr = convertTemplateArgAttr(attr, resolvingParams);
600 newParams.push_back(newAttr);
601 changed |= newAttr != attr;
607 LogicalResult validateStructType(StructType structTy, Operation *diagnosticOp)
const {
612 bool removeOwnedParams = isOwnedStructType(structTy) && params.size() == oldParamOrder_.size();
613 for (
auto indexedAttr : llvm::enumerate(params.getValue())) {
614 unsigned index = indexedAttr.index();
615 Attribute attr = indexedAttr.value();
616 if (trimResolvedParams_ && removeOwnedParams &&
617 removedParams_.contains(oldParamOrder_[index])) {
618 if (failed(checkRemovedTemplateParam(
619 oldParamOrder_[index], attr, diagnosticOp,
true,
626 if (failed(validateAttr(attr, diagnosticOp))) {
635template <
typename ConverterT,
typename SetSignatureFn>
636static void updateCallableSignature(
637 FunctionType oldFuncTy, Region &body, ConverterT &converter, SetSignatureFn setSignature
639 Type converted = converter.convertType(oldFuncTy);
640 auto newFuncTy = llvm::cast<FunctionType>(converted);
641 if (oldFuncTy == newFuncTy) {
645 setSignature(newFuncTy);
650 Block &entryBlock = body.front();
651 assert(entryBlock.getNumArguments() == newFuncTy.getNumInputs());
652 for (
auto [arg, newTy] : llvm::zip_equal(entryBlock.getArguments(), newFuncTy.getInputs())) {
662template <
typename ConverterT>
663static void updateFuncSignature(
FuncDefOp func, ConverterT &converter) {
664 updateCallableSignature(
666 [func](FunctionType newFuncTy)
mutable { func.setType(newFuncTy); }
671template <
typename ConverterT>
672static void updateContractSignature(
verif::ContractOp contract, ConverterT &converter) {
673 updateCallableSignature(
675 [contract](FunctionType newFuncTy)
mutable { contract.setFunctionType(newFuncTy); }
681template <
typename ConverterT>
static bool convertCallCallee(
CallOp callOp, ConverterT &converter) {
682 if constexpr (
requires { converter.convertCallCallee(callOp); }) {
684 SymbolRefAttr newCallee = converter.convertCallCallee(callOp);
685 if (newCallee != oldCallee) {
695template <
typename ConverterT>
696static bool convertContractTarget(
verif::ContractOp contract, ConverterT &converter) {
697 if constexpr (
requires { converter.convertContractTarget(contract); }) {
699 SymbolRefAttr newTarget = converter.convertContractTarget(contract);
700 if (newTarget != oldTarget) {
709template <
typename ConverterT>
static bool converterFailed(ConverterT &converter) {
710 if constexpr (
requires { converter.hadFailure(); }) {
711 return converter.hadFailure();
717template <
typename ConverterT>
718static void startConverterOperation(ConverterT &converter, Operation *op) {
719 if constexpr (
requires { converter.startOperation(op); }) {
720 converter.startOperation(op);
729template <
typename ConverterT>
730static FailureOr<bool> convertOperationTypes(Operation *op, ConverterT &converter) {
731 if (
auto createOp = llvm::dyn_cast<CreateArrayOp>(op)) {
732 ArrayType oldResultTy = createOp.getType();
733 auto newResultTy = llvm::dyn_cast<ArrayType>(converter.convertType(oldResultTy));
734 auto newElemTy = llvm::dyn_cast<ArrayType>(converter.convertType(oldResultTy.
getElementType()));
735 if (converterFailed(converter)) {
738 if (newResultTy && newElemTy && newResultTy != oldResultTy && !createOp.
getElements().empty() &&
739 oldResultTy.hasStaticShape()) {
742 SmallVector<Type> newElementValueTypes;
743 newElementValueTypes.reserve(createOp.
getElements().size());
744 for (
auto [index, element] : llvm::enumerate(createOp.
getElements())) {
745 Type newElementValueTy = converter.convertType(element.getType());
746 if (converterFailed(converter)) {
749 if (newElementValueTy != newElemTy) {
750 createOp.emitError() <<
"cannot rewrite initialized array.new: initializer " << index
751 <<
" converts to " << newElementValueTy <<
", but expected "
755 newElementValueTypes.push_back(newElementValueTy);
758 OpBuilder builder(createOp);
759 Location loc = createOp.getLoc();
762 for (
auto [index, element] : llvm::enumerate(createOp.
getElements())) {
763 Type newElementValueTy = newElementValueTypes[index];
764 if (element.getType() != newElementValueTy) {
765 element.setType(newElementValueTy);
767 std::optional<SmallVector<Value>> indices =
769 assert(indices &&
"static array initializer index should delinearize");
778 if (
auto readOp = llvm::dyn_cast<ReadArrayOp>(op)) {
779 Type newResultTy = converter.convertType(readOp.getResult().getType());
780 if (converterFailed(converter)) {
783 if (
auto newArrayTy = llvm::dyn_cast<ArrayType>(newResultTy)) {
784 OpBuilder builder(readOp);
786 readOp.getLoc(), newArrayTy, readOp.getArrRef(), readOp.getIndices()
788 readOp.getResult().replaceAllUsesWith(extractOp.getResult());
794 if (
auto writeOp = llvm::dyn_cast<WriteArrayOp>(op)) {
795 Type newRvalueTy = converter.convertType(writeOp.getRvalue().getType());
796 if (converterFailed(converter)) {
799 if (llvm::isa<ArrayType>(newRvalueTy)) {
800 OpBuilder builder(writeOp);
802 writeOp.getLoc(), writeOp.getArrRef(), writeOp.getIndices(), writeOp.getRvalue()
809 bool changed =
false;
811 if (
auto func = llvm::dyn_cast<FuncDefOp>(op)) {
812 FunctionType oldFuncTy = func.getFunctionType();
813 updateFuncSignature(func, converter);
814 if (converterFailed(converter)) {
817 changed |= oldFuncTy != func.getFunctionType();
819 if (
auto contract = llvm::dyn_cast<verif::ContractOp>(op)) {
821 updateContractSignature(contract, converter);
822 if (converterFailed(converter)) {
826 changed |= convertContractTarget(contract, converter);
827 if (converterFailed(converter)) {
832 for (Region ®ion : op->getRegions()) {
833 for (Block &block : region.getBlocks()) {
834 for (BlockArgument arg : block.getArguments()) {
835 Type newTy = converter.convertType(arg.getType());
836 if (converterFailed(converter)) {
839 if (newTy != arg.getType()) {
847 for (Value result : op->getResults()) {
848 Type newTy = converter.convertType(result.getType());
849 if (converterFailed(converter)) {
852 if (newTy != result.getType()) {
853 result.setType(newTy);
858 if (
auto callOp = llvm::dyn_cast<CallOp>(op)) {
859 changed |= convertCallCallee(callOp, converter);
860 if (converterFailed(converter)) {
865 SmallVector<NamedAttribute> newAttrs;
866 bool attrsChanged =
false;
867 newAttrs.reserve(op->getAttrs().size());
868 for (NamedAttribute attr : op->getAttrs()) {
869 Attribute newAttr = converter.convertAttr(attr.getValue());
870 if (converterFailed(converter)) {
873 newAttrs.emplace_back(attr.getName(), newAttr);
874 attrsChanged |= newAttr != attr.getValue();
877 op->setAttrs(DictionaryAttr::get(op->getContext(), newAttrs));
885template <
typename ConverterT>
886static FailureOr<bool> convertOperationTypesInAndTrack(Operation *root, ConverterT &converter) {
887 bool changed =
false;
888 WalkResult res = root->walk([&converter, &changed](Operation *op) {
889 startConverterOperation(converter, op);
890 FailureOr<bool> opChanged = convertOperationTypes(op, converter);
891 if (failed(opChanged)) {
892 return WalkResult::interrupt();
894 changed |= *opChanged;
895 return WalkResult::advance();
897 if (res.wasInterrupted()) {
904template <
typename ConverterT>
905static LogicalResult convertOperationTypesIn(Operation *root, ConverterT &converter) {
906 return failure(failed(convertOperationTypesInAndTrack(root, converter)));
915static void removeIdentityCasts(Operation *root) {
917 if (castOp.getInput().getType() != castOp.getResult().getType()) {
920 castOp.getResult().replaceAllUsesWith(castOp.getInput());
926template <
typename ConverterT>
927static LogicalResult convertTemplateExprTypesIn(
TemplateOp templateOp, ConverterT &converter) {
929 if (failed(convertOperationTypesIn(expr.getOperation(), converter))) {
932 removeIdentityCasts(expr.getOperation());
942static std::string buildSpecializedTemplateCloneCacheKey(
943 StringRef templateName, ArrayRef<StringAttr> oldParamOrder,
944 const DenseMap<Attribute, Attribute> ¶mNameToConcrete
947 llvm::raw_string_ostream os(key);
948 os << templateName.size() <<
':' << templateName;
949 for (StringAttr paramName : oldParamOrder) {
951 os << paramName.getValue().size() <<
':' << paramName.getValue() <<
'=';
952 auto concreteIt = paramNameToConcrete.find(FlatSymbolRefAttr::get(paramName));
953 if (concreteIt == paramNameToConcrete.end()) {
958 std::string attrText;
959 llvm::raw_string_ostream attrOs(attrText);
960 concreteIt->second.print(attrOs);
961 os << attrText.size() <<
':' << attrText;
967static bool canCopyTemplateExprToSpecialization(
974 return WalkResult::interrupt();
976 return WalkResult::advance();
985static void copyPreservedTemplateExprs(
986 TemplateOp parentTemplate, Block &newTemplateBody, ArrayRef<Attribute> remainingNames
988 DenseSet<StringAttr> availableNames;
989 for (Attribute name : remainingNames) {
990 FlatSymbolRefAttr nameSym = llvm::cast<FlatSymbolRefAttr>(name);
991 availableNames.insert(nameSym.getAttr());
995 if (canCopyTemplateExprToSpecialization(expr, availableNames)) {
996 newTemplateBody.push_back(expr->clone());
1002static FailureOr<TemplateOp> getOrCreateSpecializedTemplateClone(
1003 TemplateOp parentTemplate, ArrayRef<StringAttr> oldParamOrder,
1004 const DenseMap<Attribute, Attribute> ¶mNameToConcrete, ArrayAttr callParams,
1005 SymbolTableCollection &tables, llvm::StringMap<StringAttr> &templateClones,
1009 std::string cacheKey = buildSpecializedTemplateCloneCacheKey(
1010 parentTemplate.
getSymName(), oldParamOrder, paramNameToConcrete
1014 if (!parentModule) {
1017 SymbolTable &moduleSymbols = tables.getSymbolTable(parentModule);
1019 auto cachedName = templateClones.find(cacheKey);
1020 if (cachedName != templateClones.end()) {
1021 Operation *existing = moduleSymbols.lookup(cachedName->second);
1022 if (
auto existingTemplate = llvm::dyn_cast_or_null<TemplateOp>(existing)) {
1023 return existingTemplate;
1028 TemplateOp newTemplate = parentTemplate.cloneWithoutRegions();
1030 assert(newTemplate->getNumRegions() > 0 &&
"region exists");
1032 Block &newTemplateBody = newTemplate.
getBodyRegion().front();
1033 SymbolTable &parentTemplateSymbols = tables.getSymbolTable(parentTemplate);
1035 FlatSymbolRefAttr nameSym = llvm::cast<FlatSymbolRefAttr>(name);
1036 Operation *paramOp = parentTemplateSymbols.lookup(nameSym.getAttr());
1037 assert(paramOp &&
"symbol must exist");
1038 newTemplateBody.push_back(paramOp->clone());
1040 copyPreservedTemplateExprs(parentTemplate, newTemplateBody, layout.
remainingNames);
1042 moduleSymbols.insert(newTemplate, Block::iterator(parentTemplate));
1043 templateClones.try_emplace(cacheKey, newTemplate.
getSymNameAttr());
1053class ConcreteStructInstantiationConverter {
1055 struct StructInstantiationTypes {
1057 StructType localType;
1059 StructType remoteType;
1067 SymbolTableCollection &tables_;
1069 DenseMap<StructType, StructInstantiationTypes> instantiations_;
1071 SpecializedTemplateCloneCache templateClones_;
1073 DenseSet<SymbolRefAttr> instantiatedCloneNames_;
1075 DenseMap<StructType, StructType> activeLocalStructReplacements_;
1077 DenseMap<StringAttr, Type> activeTypeReplacements_;
1079 bool hasFailure =
false;
1083 ConcreteStructInstantiationConverter(MLIRContext *c, ModuleOp m, SymbolTableCollection &t)
1084 : ctx_(c), module_(m), tables_(t) {}
1087 bool hadFailure()
const {
return hasFailure; }
1090 Type convertType(Type ty) {
1091 if (!ty || hasFailure) {
1094 if (
auto tvarTy = llvm::dyn_cast<TypeVarType>(ty)) {
1095 auto it = activeTypeReplacements_.find(tvarTy.getNameRef().getAttr());
1096 return it == activeTypeReplacements_.end() ? ty : it->second;
1098 if (
auto arrTy = llvm::dyn_cast<ArrayType>(ty)) {
1099 return convertArrayElementType(arrTy, [
this](Type elemTy) {
return convertType(elemTy); });
1101 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
1102 return convertPodType(podTy, ctx_, *
this);
1104 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
1105 return convertFunctionType(funcTy, *
this);
1107 if (
auto structTy = llvm::dyn_cast<StructType>(ty)) {
1108 return convertStructType(structTy);
1114 Attribute convertAttr(Attribute attr) {
1118 return convertTypeOrArrayAttr(attr, ctx_, [
this](Type ty) {
1119 return convertType(ty);
1120 }, [
this](Attribute nested) {
return convertTemplateArgAttr(nested); });
1124 SymbolRefAttr convertCallCallee(CallOp callOp) {
1126 if (!callee || callee.getNestedReferences().empty()) {
1130 StructType targetStructTy = getStructFunctionTargetType(callOp);
1131 if (!targetStructTy) {
1134 auto convertedStructTy = llvm::dyn_cast<StructType>(convertType(targetStructTy));
1135 if (!convertedStructTy) {
1139 SymbolRefAttr convertedStructName = convertedStructTy.getNameRef();
1143 SmallVector<FlatSymbolRefAttr> pieces =
getPieces(convertedStructName);
1144 pieces.push_back(FlatSymbolRefAttr::get(callee.getLeafReference()));
1149 SymbolRefAttr convertContractTarget(verif::ContractOp contract) {
1156 if (contractTy.getNumInputs() == 0) {
1159 auto selfTy = llvm::dyn_cast<StructType>(contractTy.getInput(0));
1160 if (!selfTy || selfTy.getNameRef() == target) {
1163 return instantiatedCloneNames_.contains(selfTy.getNameRef()) ? selfTy.getNameRef() : target;
1168 static StructType getStructFunctionTargetType(CallOp callOp) {
1169 StringAttr calleeLeaf = callOp.
getCallee().getLeafReference();
1180 Attribute convertTemplateArgAttr(Attribute attr) {
1181 if (StringAttr symbolName = getFlatSymbolName(attr)) {
1182 auto it = activeTypeReplacements_.find(symbolName);
1183 if (it != activeTypeReplacements_.end()) {
1184 return TypeAttr::get(it->second);
1187 return convertAttr(attr);
1191 StructType convertStructType(StructType structTy) {
1192 ArrayAttr params = structTy.
getParams();
1197 SmallVector<Attribute> newParams;
1198 bool changed =
false;
1199 for (Attribute attr : params.getValue()) {
1200 Attribute newAttr = convertTemplateArgAttr(attr);
1201 newParams.push_back(newAttr);
1202 changed |= newAttr != attr;
1204 StructType convertedTy =
1208 if (
auto it = activeLocalStructReplacements_.find(convertedTy);
1209 it != activeLocalStructReplacements_.end()) {
1212 if (instantiatedCloneNames_.contains(convertedTy.
getNameRef())) {
1216 if (llvm::any_of(newParams, [](Attribute attr) {
1222 FailureOr<StructType> cloneTy = getOrCreateStructClone(convertedTy, newParams);
1223 if (failed(cloneTy)) {
1231 FailureOr<StructType>
1232 getOrCreateStructClone(StructType concreteStructTy, ArrayRef<Attribute> concreteParams) {
1233 if (
auto it = instantiations_.find(concreteStructTy); it != instantiations_.end()) {
1234 return it->second.remoteType;
1237 FailureOr<SymbolLookupResult<StructDefOp>> lookup =
1239 if (failed(lookup)) {
1243 StructDefOp origStruct = lookup->get();
1245 if (!parentTemplate) {
1248 StructType typeAtDef = origStruct.
getType();
1249 ArrayAttr paramNames = typeAtDef.
getParams();
1250 if (!paramNames || paramNames.size() != concreteParams.size()) {
1254 DenseMap<StringAttr, Type> typeReplacements;
1255 DenseMap<Attribute, Attribute> paramNameToConcrete;
1256 SmallVector<StringAttr> oldParamOrder;
1257 oldParamOrder.reserve(paramNames.size());
1258 SmallVector<Attribute> convertedSourceParams;
1259 convertedSourceParams.reserve(concreteParams.size());
1260 for (
auto [paramName, concreteAttr] : llvm::zip_equal(paramNames.getValue(), concreteParams)) {
1261 auto paramSym = llvm::dyn_cast<FlatSymbolRefAttr>(paramName);
1265 oldParamOrder.push_back(paramSym.getAttr());
1266 auto concreteType = llvm::dyn_cast<TypeAttr>(concreteAttr);
1268 paramNameToConcrete.try_emplace(FlatSymbolRefAttr::get(paramSym.getAttr()), concreteAttr);
1269 typeReplacements.try_emplace(paramSym.getAttr(), concreteType.getValue());
1270 convertedSourceParams.push_back(concreteType);
1272 convertedSourceParams.push_back(paramName);
1275 if (paramNameToConcrete.empty()) {
1276 return concreteStructTy;
1279 InstantiationLayout layout;
1280 ArrayAttr concreteParamArray = ArrayAttr::get(ctx_, concreteParams);
1281 FailureOr<TemplateOp> newTemplate = getOrCreateSpecializedTemplateClone(
1282 parentTemplate, oldParamOrder, paramNameToConcrete, concreteParamArray, tables_,
1283 templateClones_[parentTemplate.getOperation()], layout
1285 if (failed(newTemplate)) {
1289 SymbolTable &templateSymbols = tables_.getSymbolTable(*newTemplate);
1290 StructDefOp clone = origStruct.clone();
1291 templateSymbols.insert(clone);
1292 StructType localTy =
1294 StructType remoteTy =
1296 instantiations_.try_emplace(concreteStructTy, StructInstantiationTypes {localTy, remoteTy});
1299 DenseMap<StringAttr, Type> previousReplacements = std::move(activeTypeReplacements_);
1300 DenseMap<StructType, StructType> previousLocalStructReplacements =
1301 std::move(activeLocalStructReplacements_);
1302 activeTypeReplacements_ = std::move(typeReplacements);
1303 activeLocalStructReplacements_.clear();
1304 activeLocalStructReplacements_.try_emplace(concreteStructTy, localTy);
1305 activeLocalStructReplacements_.try_emplace(
1309 activeLocalStructReplacements_.try_emplace(
1312 activeLocalStructReplacements_.try_emplace(
1316 if (failed(convertTemplateExprTypesIn(*newTemplate, *
this))) {
1317 activeLocalStructReplacements_ = std::move(previousLocalStructReplacements);
1318 activeTypeReplacements_ = std::move(previousReplacements);
1319 instantiations_.erase(concreteStructTy);
1320 instantiatedCloneNames_.erase(cloneNameRef);
1324 if (failed(convertOperationTypesIn(clone.getOperation(), *
this))) {
1325 activeLocalStructReplacements_ = std::move(previousLocalStructReplacements);
1326 activeTypeReplacements_ = std::move(previousReplacements);
1327 instantiations_.erase(concreteStructTy);
1328 instantiatedCloneNames_.erase(cloneNameRef);
1332 removeIdentityCasts(clone.getOperation());
1333 activeLocalStructReplacements_ = std::move(previousLocalStructReplacements);
1334 activeTypeReplacements_ = std::move(previousReplacements);
1340struct TemplateInferenceInfo {
1342 TemplateOp templateOp;
1344 SmallVector<StringAttr> templatePath;
1346 SmallVector<StringAttr> oldParamOrder;
1348 DenseMap<StringAttr, TemplateParamOp> typeVarParams;
1350 DenseMap<StringAttr, InferredType> replacements;
1352 DenseMap<StringAttr, InferredType> templateScopeReplacements;
1354 DenseMap<Operation *, DenseMap<StringAttr, InferredType>> functionReplacements;
1358static DenseMap<Attribute, Attribute>
1359buildParamNameToCallArg(ArrayAttr callParams, ArrayRef<StringAttr> oldParamOrder) {
1360 DenseMap<Attribute, Attribute> paramNameToCallArg;
1361 if (!callParams || callParams.size() != oldParamOrder.size()) {
1362 return paramNameToCallArg;
1364 for (
auto [paramName, attr] : llvm::zip_equal(oldParamOrder, callParams.getValue())) {
1365 paramNameToCallArg.try_emplace(FlatSymbolRefAttr::get(paramName), attr);
1367 return paramNameToCallArg;
1377substituteExplicitCallTypeArgs(Type ty, ArrayAttr callParams, ArrayRef<StringAttr> oldParamOrder) {
1378 if (!callParams || callParams.size() != oldParamOrder.size()) {
1382 DenseMap<StringAttr, Type> callTypeArgs;
1383 for (
auto [paramName, attr] : llvm::zip_equal(oldParamOrder, callParams.getValue())) {
1384 if (
auto tyAttr = llvm::dyn_cast<TypeAttr>(attr)) {
1385 callTypeArgs.try_emplace(paramName, tyAttr.getValue());
1388 TypeVarReplacementConverter converter(
1389 ty.getContext(), ArrayRef<StringAttr> {}, oldParamOrder, callTypeArgs,
1392 return converter.convertType(ty);
1396class ExplicitCallTemplateParamSubstituter {
1398 const DenseMap<Attribute, Attribute> ¶mNameToCallArg_;
1400 DenseSet<Attribute> resolvingParams_;
1403 explicit ExplicitCallTemplateParamSubstituter(
1404 const DenseMap<Attribute, Attribute> ¶mNameToCallArg
1406 : paramNameToCallArg_(paramNameToCallArg) {}
1408 Type substituteType(Type ty);
1409 Attribute substituteAttr(Attribute attr);
1413Attribute ExplicitCallTemplateParamSubstituter::substituteAttr(Attribute attr) {
1417 auto it = paramNameToCallArg_.find(attr);
1418 if (it != paramNameToCallArg_.end()) {
1421 if (
auto tyAttr = llvm::dyn_cast<TypeAttr>(attr)) {
1422 Type newTy = substituteType(tyAttr.getValue());
1423 return newTy == tyAttr.getValue() ? attr : TypeAttr::get(newTy);
1425 if (
auto arrAttr = llvm::dyn_cast<ArrayAttr>(attr)) {
1426 SmallVector<Attribute> newAttrs;
1427 bool changed =
false;
1428 for (Attribute nested : arrAttr.getValue()) {
1429 Attribute newNested = substituteAttr(nested);
1430 newAttrs.push_back(newNested);
1431 changed |= newNested != nested;
1433 return changed ? ArrayAttr::get(attr.getContext(), newAttrs) : attr;
1445Type ExplicitCallTemplateParamSubstituter::substituteType(Type ty) {
1449 if (
auto tvarTy = llvm::dyn_cast<TypeVarType>(ty)) {
1450 Attribute paramRef = tvarTy.getNameRef();
1451 auto it = paramNameToCallArg_.find(paramRef);
1452 if (it == paramNameToCallArg_.end()) {
1455 auto tyAttr = llvm::dyn_cast<TypeAttr>(it->second);
1456 if (!tyAttr || tyAttr.getValue() == ty) {
1459 if (!resolvingParams_.insert(paramRef).second) {
1462 Type replacement = substituteType(tyAttr.getValue());
1463 resolvingParams_.erase(paramRef);
1466 if (
auto arrTy = llvm::dyn_cast<ArrayType>(ty)) {
1467 SmallVector<Attribute> newDims;
1468 bool changed =
false;
1470 Attribute newDim = substituteAttr(dim);
1471 newDims.push_back(newDim);
1472 changed |= newDim != dim;
1482 if (
auto structTy = llvm::dyn_cast<StructType>(ty)) {
1483 ArrayAttr params = structTy.
getParams();
1487 SmallVector<Attribute> newParams;
1488 bool changed =
false;
1489 for (Attribute param : params.getValue()) {
1490 Attribute newParam = substituteAttr(param);
1491 newParams.push_back(newParam);
1492 changed |= newParam != param;
1497 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
1498 SmallVector<RecordAttr> newRecords;
1499 bool changed =
false;
1500 for (RecordAttr record : podTy.
getRecords()) {
1501 Type newRecordTy = substituteType(record.getType());
1502 newRecords.push_back(RecordAttr::get(ty.getContext(), record.getName(), newRecordTy));
1503 changed |= newRecordTy != record.getType();
1505 return changed ?
PodType::get(ty.getContext(), newRecords) : podTy;
1507 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
1508 SmallVector<Type> newInputs;
1509 SmallVector<Type> newResults;
1510 bool changed =
false;
1511 newInputs.reserve(funcTy.getNumInputs());
1512 newResults.reserve(funcTy.getNumResults());
1513 for (Type inputTy : funcTy.getInputs()) {
1514 Type newInputTy = substituteType(inputTy);
1515 newInputs.push_back(newInputTy);
1516 changed |= newInputTy != inputTy;
1518 for (Type resultTy : funcTy.getResults()) {
1519 Type newResultTy = substituteType(resultTy);
1520 newResults.push_back(newResultTy);
1521 changed |= newResultTy != resultTy;
1523 return changed ? FunctionType::get(ty.getContext(), newInputs, newResults) : funcTy;
1529static Type substituteExplicitCallTemplateParams(
1530 Type ty, ArrayAttr callParams, ArrayRef<StringAttr> oldParamOrder
1532 auto paramNameToCallArg = buildParamNameToCallArg(callParams, oldParamOrder);
1533 ExplicitCallTemplateParamSubstituter substituter(paramNameToCallArg);
1534 return substituter.substituteType(ty);
1538static bool symbolMatchesParam(SymbolRefAttr symRef, StringAttr paramName) {
1539 return symRef && symRef.getNestedReferences().empty() && symRef.getRootReference() == paramName;
1543class ParamMentionChecker {
1544 StringAttr paramName_;
1547 explicit ParamMentionChecker(StringAttr paramName) : paramName_(paramName) {}
1550 bool typeMentions(Type ty)
const {
1554 if (
auto tvarTy = llvm::dyn_cast<TypeVarType>(ty)) {
1555 return tvarTy.getNameRef().getAttr() == paramName_;
1557 if (
auto arrayTy = llvm::dyn_cast<ArrayType>(ty)) {
1558 return typeMentions(arrayTy.getElementType()) ||
1559 llvm::any_of(arrayTy.getDimensionSizes(), [
this](Attribute dim) {
1560 return attrMentions(dim, true);
1563 if (
auto structTy = llvm::dyn_cast<StructType>(ty)) {
1564 ArrayAttr params = structTy.
getParams();
1565 return params && attrMentions(params,
true);
1567 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
1568 return llvm::any_of(podTy.
getRecords(), [
this](RecordAttr record) {
1569 return typeMentions(record.getType());
1572 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
1573 return llvm::any_of(funcTy.getInputs(), [
this](Type input) {
1574 return typeMentions(input);
1575 }) || llvm::any_of(funcTy.getResults(), [
this](Type result) { return typeMentions(result); });
1585 bool attrMentions(Attribute attr,
bool allowSymbolRefs)
const {
1589 if (
auto typeAttr = llvm::dyn_cast<TypeAttr>(attr)) {
1590 return typeMentions(typeAttr.getValue());
1592 if (
auto arrayAttr = llvm::dyn_cast<ArrayAttr>(attr)) {
1593 return llvm::any_of(arrayAttr, [
this](Attribute nested) {
1594 return attrMentions(nested,
true);
1597 return allowSymbolRefs && symbolMatchesParam(llvm::dyn_cast<SymbolRefAttr>(attr), paramName_);
1602static bool operationMentionsParam(Operation *op, StringAttr paramName) {
1603 ParamMentionChecker mentions(paramName);
1604 if (llvm::any_of(op->getResultTypes(), [&mentions](Type ty) {
1605 return mentions.typeMentions(ty);
1609 for (Region ®ion : op->getRegions()) {
1610 for (Block &block : region.getBlocks()) {
1611 if (llvm::any_of(block.getArgumentTypes(), [&mentions](Type ty) {
1612 return mentions.typeMentions(ty);
1618 return llvm::any_of(op->getAttrs(), [&mentions](NamedAttribute attr) {
1619 return mentions.attrMentions(attr.getValue(), false);
1624static SmallVector<StringAttr> getMentionedTypeVarParams(
1625 Attribute attr,
const DenseMap<StringAttr, TemplateParamOp> &typeVarParams
1627 SmallVector<StringAttr> mentionedParams;
1628 for (
const auto &entry : typeVarParams) {
1629 if (ParamMentionChecker(entry.first).attrMentions(attr,
true)) {
1630 mentionedParams.push_back(entry.first);
1633 return mentionedParams;
1637static bool attrMentionsAnyTypeVarParam(
1638 Attribute attr,
const DenseMap<StringAttr, TemplateParamOp> &typeVarParams
1640 return !getMentionedTypeVarParams(attr, typeVarParams).empty();
1644static bool funcMentionsParam(FuncDefOp func, StringAttr paramName) {
1645 if (operationMentionsParam(func.getOperation(), paramName)) {
1648 WalkResult result = func.walk([paramName](Operation *op) {
1649 return operationMentionsParam(op, paramName) ? WalkResult::interrupt() : WalkResult::advance();
1651 return result.wasInterrupted();
1655static bool contractMentionsParam(verif::ContractOp contract, StringAttr paramName) {
1656 if (operationMentionsParam(contract.getOperation(), paramName)) {
1659 WalkResult result = contract.walk([paramName](Operation *op) {
1660 return operationMentionsParam(op, paramName) ? WalkResult::interrupt() : WalkResult::advance();
1662 return result.wasInterrupted();
1666static bool targetMentionsParam(FuncDefOp func, StringAttr paramName) {
1667 return funcMentionsParam(func, paramName);
1671static bool targetMentionsParam(verif::ContractOp contract, StringAttr paramName) {
1672 return contractMentionsParam(contract, paramName);
1676static bool exprMentionsParam(TemplateExprOp expr, StringAttr paramName) {
1677 WalkResult result = expr.walk([paramName](Operation *op) {
1678 return operationMentionsParam(op, paramName) ? WalkResult::interrupt() : WalkResult::advance();
1680 return result.wasInterrupted();
1685static bool structUseCoveredByFunctionProof(
1686 StructDefOp structOp, StringAttr paramName, Type replacementType,
1687 const DenseMap<Operation *, DenseMap<StringAttr, InferredType>> &functionReplacements
1689 bool sawMention =
false;
1690 bool missingProof =
false;
1691 structOp.walk([&](FuncDefOp func) {
1692 if (func->getParentOfType<StructDefOp>() != structOp || !funcMentionsParam(func, paramName)) {
1696 auto funcIt = functionReplacements.find(func.getOperation());
1697 if (funcIt == functionReplacements.end()) {
1698 missingProof =
true;
1701 auto replacementIt = funcIt->second.find(paramName);
1702 if (replacementIt == funcIt->second.end() || replacementIt->second.type != replacementType) {
1703 missingProof =
true;
1706 return sawMention && !missingProof;
1716static bool hasUncoveredNonFunctionMention(
1717 TemplateOp templateOp, StringAttr paramName, Type replacementType,
1718 const DenseMap<Operation *, DenseMap<StringAttr, InferredType>> &functionReplacements
1721 templateOp.walk([paramName, replacementType, &functionReplacements](Operation *op) {
1722 if (llvm::isa<FuncDefOp, TemplateExprOp, TemplateParamOp, verif::ContractOp>(op) ||
1724 return WalkResult::advance();
1726 if (!operationMentionsParam(op, paramName)) {
1727 return WalkResult::advance();
1729 if (StructDefOp structOp = op->getParentOfType<StructDefOp>()) {
1730 if (structUseCoveredByFunctionProof(
1731 structOp, paramName, replacementType, functionReplacements
1733 return WalkResult::advance();
1736 return WalkResult::interrupt();
1738 return result.wasInterrupted();
1747class TypeVarInferenceCollector {
1749 TemplateInferenceInfo &inferenceInfo;
1751 DenseMap<StringAttr, InferredType> &replacements;
1753 DenseMap<Value, InferredType> byValue;
1755 DenseMap<StringAttr, DenseSet<StringAttr>> paramRelations;
1757 bool changedInIteration =
false;
1761 TypeVarInferenceCollector(
1762 TemplateInferenceInfo &info, DenseMap<StringAttr, InferredType> &scopeReplacements
1764 : inferenceInfo(info), replacements(scopeReplacements) {}
1767 LogicalResult collect(Operation *root) {
1769 changedInIteration =
false;
1770 WalkResult result = root->walk([
this](UnifiableCastOp castOp) {
1771 return WalkResult(collectTypePairInferences(
1776 if (result.wasInterrupted()) {
1779 }
while (changedInIteration);
1784 LogicalResult collectTypeInferences(Type lhs, Type rhs, Location loc) {
1786 changedInIteration =
false;
1787 if (failed(collectTypePairInferences(lhs, rhs, Value(), Value(), loc))) {
1790 }
while (changedInIteration);
1802 LogicalResult collectStructTemplateParamInferences(
1803 Operation *root, ModuleOp module, DenseMap<Operation *, TemplateInferenceInfo *> &infos,
1804 SymbolTableCollection &tables
1806 WalkResult result = root->walk([&](Operation *op) {
1807 return failed(collectOperationStructTemplateParamInferences(op, module, infos, tables))
1808 ? WalkResult::interrupt()
1809 : WalkResult::advance();
1811 return failure(result.wasInterrupted());
1815 LogicalResult collectOperationStructTemplateParamInferences(
1816 Operation *op, ModuleOp module, DenseMap<Operation *, TemplateInferenceInfo *> &infos,
1817 SymbolTableCollection &tables
1819 Location loc = op->getLoc();
1821 if (
auto func = llvm::dyn_cast<FuncDefOp>(op)) {
1822 if (failed(collectStructTemplateParamInferences(
1829 for (Region ®ion : op->getRegions()) {
1830 for (Block &block : region.getBlocks()) {
1831 for (Type argTy : block.getArgumentTypes()) {
1832 if (failed(collectStructTemplateParamInferences(argTy, loc, module, infos, tables))) {
1839 for (Type resultTy : op->getResultTypes()) {
1840 if (failed(collectStructTemplateParamInferences(resultTy, loc, module, infos, tables))) {
1845 if (
auto callOp = llvm::dyn_cast<CallOp>(op)) {
1846 if (failed(collectCallableTemplateParamInferences(callOp, infos, tables))) {
1850 if (
auto includeOp = llvm::dyn_cast<verif::IncludeOp>(op)) {
1851 if (failed(collectIncludeTemplateParamInferences(includeOp, infos, tables))) {
1856 for (NamedAttribute attr : op->getAttrs()) {
1858 collectStructTemplateParamInferences(attr.getValue(), loc, module, infos, tables)
1872 LogicalResult collectStructTemplateParamInferences(
1873 Attribute attr, Location loc, ModuleOp module,
1874 DenseMap<Operation *, TemplateInferenceInfo *> &infos, SymbolTableCollection &tables
1879 if (
auto tyAttr = llvm::dyn_cast<TypeAttr>(attr)) {
1880 return collectStructTemplateParamInferences(tyAttr.getValue(), loc, module, infos, tables);
1882 if (
auto arrAttr = llvm::dyn_cast<ArrayAttr>(attr)) {
1883 for (Attribute nested : arrAttr.getValue()) {
1884 if (failed(collectStructTemplateParamInferences(nested, loc, module, infos, tables))) {
1900 LogicalResult collectStructTemplateParamInferences(
1901 Type ty, Location loc, ModuleOp module, DenseMap<Operation *, TemplateInferenceInfo *> &infos,
1902 SymbolTableCollection &tables
1907 if (
auto arrTy = llvm::dyn_cast<ArrayType>(ty)) {
1908 return collectStructTemplateParamInferences(
1912 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
1913 for (RecordAttr record : podTy.
getRecords()) {
1915 collectStructTemplateParamInferences(record.getType(), loc, module, infos, tables)
1922 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
1923 for (Type inputTy : funcTy.getInputs()) {
1924 if (failed(collectStructTemplateParamInferences(inputTy, loc, module, infos, tables))) {
1928 for (Type resultTy : funcTy.getResults()) {
1929 if (failed(collectStructTemplateParamInferences(resultTy, loc, module, infos, tables))) {
1935 auto structTy = llvm::dyn_cast<StructType>(ty);
1940 ArrayAttr params = structTy.
getParams();
1944 for (Attribute attr : params.getValue()) {
1945 if (failed(collectStructTemplateParamInferences(attr, loc, module, infos, tables))) {
1950 FailureOr<SymbolLookupResult<StructDefOp>> lookup =
1952 if (failed(lookup)) {
1956 if (!parentTemplate) {
1959 TemplateInferenceInfo *targetInfo = infos.lookup(parentTemplate.getOperation());
1960 if (!targetInfo || targetInfo->replacements.empty() ||
1961 params.size() != targetInfo->oldParamOrder.size()) {
1965 for (
auto [paramName, attr] : llvm::zip_equal(targetInfo->oldParamOrder, params.getValue())) {
1966 auto replacementIt = targetInfo->replacements.find(paramName);
1967 if (replacementIt == targetInfo->replacements.end()) {
1970 Type expectedTy = substituteExplicitCallTemplateParams(
1971 replacementIt->second.type, params, targetInfo->oldParamOrder
1973 Attribute expectedAttr = TypeAttr::get(expectedTy);
1974 if (failed(collectTemplateArgInferences(attr, expectedAttr, loc))) {
1987 SmallVector<Attribute>
1988 getInferredOmittedTemplateArgs(
const UnificationMap &unifyResult, StringAttr targetParamName) {
1989 SmallVector<Attribute> inferredAttrs;
1990 auto targetRef = FlatSymbolRefAttr::get(targetParamName);
1991 auto inferredIt = unifyResult.find({targetRef, Side::RHS});
1992 if (inferredIt != unifyResult.end()) {
1993 inferredAttrs.push_back(inferredIt->second);
1995 for (
const auto &entry : unifyResult) {
1996 if (entry.first.second == Side::LHS && entry.second == targetRef) {
1997 inferredAttrs.push_back(entry.first.first);
2000 return inferredAttrs;
2004 template <
typename TargetOp>
2005 TemplateInferenceInfo *
2006 getTargetTemplateInfo(TargetOp targetOp, DenseMap<Operation *, TemplateInferenceInfo *> &infos) {
2008 if (!parentTemplate) {
2011 TemplateInferenceInfo *targetInfo = infos.lookup(parentTemplate.getOperation());
2012 if (!targetInfo || targetInfo->replacements.empty()) {
2025 template <
typename CallableOp>
2026 LogicalResult collectCallableUseTemplateParamInferences(
2027 CallableOp callableOp, FunctionType targetSignature, TemplateInferenceInfo &targetInfo
2029 ArrayAttr params = callableOp.getTemplateParamsAttr();
2031 if (params.size() != targetInfo.oldParamOrder.size()) {
2034 struct DeferredExplicitArgInference {
2036 Attribute expectedAttr;
2037 SmallVector<StringAttr> mentionedParams;
2039 SmallVector<DeferredExplicitArgInference> deferredExplicitArgInferences;
2040 bool deferredToSignature =
false;
2041 for (
auto [paramName, attr] : llvm::zip_equal(targetInfo.oldParamOrder, params)) {
2042 auto replacementIt = targetInfo.replacements.find(paramName);
2043 if (replacementIt == targetInfo.replacements.end()) {
2046 Type expectedTy = substituteExplicitCallTemplateParams(
2047 replacementIt->second.type, params, targetInfo.oldParamOrder
2049 Attribute expectedAttr = TypeAttr::get(expectedTy);
2050 if (attrMentionsAnyTypeVarParam(attr, inferenceInfo.typeVarParams)) {
2051 deferredToSignature =
true;
2052 SmallVector<StringAttr> mentionedParams =
2053 getMentionedTypeVarParams(attr, inferenceInfo.typeVarParams);
2054 if (!ParamMentionChecker(paramName).typeMentions(targetSignature)) {
2055 if (failed(collectTemplateArgInferences(attr, expectedAttr, callableOp.getLoc()))) {
2059 deferredExplicitArgInferences.push_back(
2060 {attr, expectedAttr, std::move(mentionedParams)}
2065 if (failed(collectTemplateArgInferences(attr, expectedAttr, callableOp.getLoc()))) {
2069 if (deferredToSignature) {
2070 DenseMap<StringAttr, Type> targetReplacements;
2071 for (
const auto &entry : targetInfo.replacements) {
2072 targetReplacements.try_emplace(entry.first, entry.second.type);
2074 TypeVarReplacementConverter converter(
2075 callableOp.getContext(), targetInfo.templatePath, targetInfo.oldParamOrder,
2076 targetReplacements,
false
2078 Type rewrittenTy = substituteExplicitCallTemplateParams(
2079 converter.convertType(targetSignature), params, targetInfo.oldParamOrder
2081 if (failed(collectTypeInferences(
2082 callableOp.getTypeSignature(), llvm::cast<FunctionType>(rewrittenTy),
2088 for (
const DeferredExplicitArgInference &deferred : deferredExplicitArgInferences) {
2089 bool signatureCanInferAllParams =
2090 llvm::all_of(deferred.mentionedParams, [&](StringAttr mentionedParam) {
2091 return ParamMentionChecker(mentionedParam).typeMentions(callableOp.getTypeSignature());
2093 bool signatureHasInferredAllParams =
2094 llvm::all_of(deferred.mentionedParams, [&](StringAttr mentionedParam) {
2095 return replacements.contains(mentionedParam);
2097 if (signatureCanInferAllParams && signatureHasInferredAllParams) {
2100 if (failed(collectTemplateArgInferences(
2101 deferred.attr, deferred.expectedAttr, callableOp.getLoc()
2110 FailureOr<UnificationMap> unifyResult = callableOp.unifyTypeSignature(targetSignature);
2111 if (failed(unifyResult)) {
2115 for (StringAttr paramName : targetInfo.oldParamOrder) {
2116 auto replacementIt = targetInfo.replacements.find(paramName);
2117 if (replacementIt == targetInfo.replacements.end()) {
2120 SmallVector<Attribute> inferredAttrs =
2121 getInferredOmittedTemplateArgs(*unifyResult, paramName);
2122 if (inferredAttrs.empty()) {
2126 Attribute expectedAttr = TypeAttr::get(replacementIt->second.type);
2127 for (Attribute inferredAttr : inferredAttrs) {
2128 if (!inferredAttr || !
typeParamsUnify({inferredAttr}, {expectedAttr})) {
2129 InFlightDiagnostic diag = callableOp.emitError()
2130 <<
"implicit template argument for inferred parameter @"
2131 << paramName.getValue() <<
" must match inferred type "
2132 << replacementIt->second.type <<
", but found ";
2133 if (
auto typeAttr = llvm::dyn_cast_if_present<TypeAttr>(inferredAttr)) {
2134 diag << typeAttr.getValue();
2136 diag << inferredAttr;
2140 if (failed(collectTemplateArgInferences(inferredAttr, expectedAttr, callableOp.getLoc()))) {
2157 LogicalResult collectCallableTemplateParamInferences(
2158 CallOp callOp, DenseMap<Operation *, TemplateInferenceInfo *> &infos,
2159 SymbolTableCollection &tables
2161 FailureOr<SymbolLookupResult<FuncDefOp>> target = callOp.
getCalleeTarget(tables);
2162 if (failed(target)) {
2165 FuncDefOp targetFunc = target->get();
2166 TemplateInferenceInfo *targetInfo = getTargetTemplateInfo(targetFunc, infos);
2170 return collectCallableUseTemplateParamInferences(
2181 LogicalResult collectIncludeTemplateParamInferences(
2182 verif::IncludeOp includeOp, DenseMap<Operation *, TemplateInferenceInfo *> &infos,
2183 SymbolTableCollection &tables
2185 FailureOr<SymbolLookupResult<verif::ContractOp>> target = includeOp.
getCalleeTarget(tables);
2186 if (failed(target)) {
2189 verif::ContractOp targetContract = target->get();
2190 TemplateInferenceInfo *targetInfo = getTargetTemplateInfo(targetContract, infos);
2194 return collectCallableUseTemplateParamInferences(
2212 LogicalResult recordInference(StringAttr paramName, Type inferredTy, Value value, Location loc) {
2213 if (!inferenceInfo.typeVarParams.contains(paramName)) {
2216 if (
auto inferredTvar = llvm::dyn_cast<TypeVarType>(inferredTy)) {
2217 return recordParamRelation(paramName, inferredTvar.getNameRef().getAttr(), loc);
2223 auto reportConflict = [&](StringRef kind, Location originalLoc, Type originalTy) {
2224 InFlightDiagnostic diag = emitError(loc) <<
"conflicting inferred type for " << kind <<
" @"
2225 << paramName.getValue() <<
": " << originalTy
2226 <<
" vs " << inferredTy;
2227 diag.attachNote(originalLoc) <<
"previous inference here";
2231 auto byParamIt = replacements.find(paramName);
2232 bool learnedParamInference =
false;
2233 if (byParamIt == replacements.end()) {
2234 replacements.try_emplace(paramName, InferredType {inferredTy, loc});
2235 changedInIteration =
true;
2236 learnedParamInference =
true;
2237 }
else if (byParamIt->second.type != inferredTy) {
2238 return reportConflict(
"template parameter", byParamIt->second.loc, byParamIt->second.type);
2242 auto byValueIt = byValue.find(value);
2243 if (byValueIt == byValue.end()) {
2244 byValue.try_emplace(value, InferredType {inferredTy, loc});
2245 changedInIteration =
true;
2246 }
else if (byValueIt->second.type != inferredTy) {
2247 return reportConflict(
"SSA value using", byValueIt->second.loc, byValueIt->second.type);
2251 if (learnedParamInference) {
2252 auto relatedIt = paramRelations.find(paramName);
2253 if (relatedIt != paramRelations.end()) {
2254 for (StringAttr relatedParam : relatedIt->second) {
2255 if (failed(recordInference(relatedParam, inferredTy, Value(), loc))) {
2265 LogicalResult recordParamRelation(StringAttr lhsParam, StringAttr rhsParam, Location loc) {
2266 if (lhsParam == rhsParam || !inferenceInfo.typeVarParams.contains(rhsParam)) {
2270 bool inserted = paramRelations[lhsParam].insert(rhsParam).second;
2271 inserted |= paramRelations[rhsParam].insert(lhsParam).second;
2276 changedInIteration =
true;
2278 auto lhsIt = replacements.find(lhsParam);
2279 if (lhsIt != replacements.end() &&
2280 failed(recordInference(rhsParam, lhsIt->second.type, Value(), loc))) {
2283 auto rhsIt = replacements.find(rhsParam);
2284 if (rhsIt != replacements.end() &&
2285 failed(recordInference(lhsParam, rhsIt->second.type, Value(), loc))) {
2299 collectTypePairInferences(Type lhs, Type rhs, Value lhsValue, Value rhsValue, Location loc) {
2300 if (
auto lhsTvar = llvm::dyn_cast<TypeVarType>(lhs)) {
2301 if (failed(recordInference(lhsTvar.getNameRef().getAttr(), rhs, lhsValue, loc))) {
2305 auto rhsValueIt = byValue.find(rhsValue);
2306 if (rhsValueIt != byValue.end() &&
2307 failed(recordInference(
2308 lhsTvar.getNameRef().getAttr(), rhsValueIt->second.type, lhsValue, loc
2314 if (
auto rhsTvar = llvm::dyn_cast<TypeVarType>(rhs)) {
2315 if (failed(recordInference(rhsTvar.getNameRef().getAttr(), lhs, rhsValue, loc))) {
2319 auto lhsValueIt = byValue.find(lhsValue);
2320 if (lhsValueIt != byValue.end() &&
2321 failed(recordInference(
2322 rhsTvar.getNameRef().getAttr(), lhsValueIt->second.type, rhsValue, loc
2329 if (
auto lhsArr = llvm::dyn_cast<ArrayType>(lhs)) {
2330 if (
auto rhsArr = llvm::dyn_cast<ArrayType>(rhs)) {
2331 return collectTypePairInferences(
2332 lhsArr.getElementType(), rhsArr.getElementType(), Value(), Value(), loc
2337 if (
auto lhsStruct = llvm::dyn_cast<StructType>(lhs)) {
2338 if (
auto rhsStruct = llvm::dyn_cast<StructType>(rhs)) {
2339 ArrayRef<Attribute> lhsParams =
2340 lhsStruct.getParams() ? lhsStruct.getParams().getValue() : ArrayRef<Attribute> {};
2341 ArrayRef<Attribute> rhsParams =
2342 rhsStruct.getParams() ? rhsStruct.getParams().getValue() : ArrayRef<Attribute> {};
2343 if (lhsParams.size() != rhsParams.size()) {
2346 for (
auto [lhsAttr, rhsAttr] : llvm::zip_equal(lhsParams, rhsParams)) {
2347 if (failed(collectTemplateArgInferences(lhsAttr, rhsAttr, loc))) {
2354 if (
auto lhsPod = llvm::dyn_cast<PodType>(lhs)) {
2355 if (
auto rhsPod = llvm::dyn_cast<PodType>(rhs)) {
2356 ArrayRef<RecordAttr> lhsRecords = lhsPod.getRecords();
2357 ArrayRef<RecordAttr> rhsRecords = rhsPod.getRecords();
2358 if (lhsRecords.size() != rhsRecords.size()) {
2361 for (
auto [lhsRecord, rhsRecord] : llvm::zip_equal(lhsRecords, rhsRecords)) {
2362 if (lhsRecord.getName() != rhsRecord.getName()) {
2365 if (failed(collectTypePairInferences(
2366 lhsRecord.getType(), rhsRecord.getType(), Value(), Value(), loc
2374 if (
auto lhsFunc = llvm::dyn_cast<FunctionType>(lhs)) {
2375 if (
auto rhsFunc = llvm::dyn_cast<FunctionType>(rhs)) {
2376 if (lhsFunc.getNumInputs() != rhsFunc.getNumInputs() ||
2377 lhsFunc.getNumResults() != rhsFunc.getNumResults()) {
2380 for (
auto [lhsInput, rhsInput] :
2381 llvm::zip_equal(lhsFunc.getInputs(), rhsFunc.getInputs())) {
2382 if (failed(collectTypePairInferences(lhsInput, rhsInput, Value(), Value(), loc))) {
2386 for (
auto [lhsResult, rhsResult] :
2387 llvm::zip_equal(lhsFunc.getResults(), rhsFunc.getResults())) {
2388 if (failed(collectTypePairInferences(lhsResult, rhsResult, Value(), Value(), loc))) {
2405 LogicalResult collectTemplateArgInferences(Attribute lhsAttr, Attribute rhsAttr, Location loc) {
2406 auto lhsTyAttr = llvm::dyn_cast<TypeAttr>(lhsAttr);
2407 auto rhsTyAttr = llvm::dyn_cast<TypeAttr>(rhsAttr);
2408 if (lhsTyAttr && rhsTyAttr) {
2409 return collectTypePairInferences(
2410 lhsTyAttr.getValue(), rhsTyAttr.getValue(), Value(), Value(), loc
2414 if (StringAttr lhsSymbolName = getFlatSymbolName(lhsAttr)) {
2415 if (StringAttr rhsSymbolName = getFlatSymbolName(rhsAttr)) {
2416 return recordParamRelation(lhsSymbolName, rhsSymbolName, loc);
2418 if (rhsTyAttr && failed(recordInference(lhsSymbolName, rhsTyAttr.getValue(), Value(), loc))) {
2422 if (StringAttr rhsSymbolName = getFlatSymbolName(rhsAttr)) {
2423 if (lhsTyAttr && failed(recordInference(rhsSymbolName, lhsTyAttr.getValue(), Value(), loc))) {
2432static DenseMap<StringAttr, Type>
2433getFunctionProofReplacements(
const TemplateInferenceInfo &info, FuncDefOp func) {
2434 DenseMap<StringAttr, Type> replacements;
2435 auto funcIt = info.functionReplacements.find(func.getOperation());
2436 if (funcIt == info.functionReplacements.end()) {
2437 return replacements;
2439 for (
const auto &entry : funcIt->second) {
2440 replacements.try_emplace(entry.first, entry.second.type);
2442 return replacements;
2446static std::optional<unsigned>
2447getParamIndex(ArrayRef<StringAttr> paramOrder, StringAttr paramName) {
2448 for (
auto indexedParam : llvm::enumerate(paramOrder)) {
2449 if (indexedParam.value() == paramName) {
2450 return indexedParam.index();
2453 return std::nullopt;
2458static DenseMap<Attribute, Attribute>
2459buildParamNameToConcrete(
const DenseMap<StringAttr, Type> &replacements) {
2460 DenseMap<Attribute, Attribute> paramNameToConcrete;
2461 for (
const auto &entry : replacements) {
2462 paramNameToConcrete.try_emplace(
2463 FlatSymbolRefAttr::get(entry.first), TypeAttr::get(entry.second)
2466 return paramNameToConcrete;
2472static bool callParamsMatchReplacements(
2473 ArrayAttr callParams, ArrayRef<StringAttr> oldParamOrder,
2474 const DenseMap<StringAttr, Type> &replacements,
2475 const DenseMap<StringAttr, InferredType> *wildcardAllowedReplacements =
nullptr
2477 if (!callParams || callParams.size() != oldParamOrder.size()) {
2480 for (
const auto &entry : replacements) {
2481 std::optional<unsigned> index = getParamIndex(oldParamOrder, entry.first);
2485 Type expectedTy = substituteExplicitCallTypeArgs(entry.second, callParams, oldParamOrder);
2486 Attribute attr = callParams[*index];
2487 if (
auto intAttr = llvm::dyn_cast<IntegerAttr>(attr);
2488 intAttr &&
isDynamic(intAttr) && wildcardAllowedReplacements &&
2489 wildcardAllowedReplacements->contains(entry.first)) {
2492 if (!templateArgUnifiesWithType(attr, expectedTy)) {
2505static LogicalResult diagnoseCallParamsMismatch(
2506 Operation *callableOp, ArrayAttr callParams, ArrayRef<StringAttr> oldParamOrder,
2507 const DenseMap<StringAttr, Type> &replacements,
2508 const DenseMap<StringAttr, InferredType> *wildcardAllowedReplacements,
bool explicitCallParams
2510 if (callParamsMatchReplacements(
2511 callParams, oldParamOrder, replacements, wildcardAllowedReplacements
2515 for (
const auto &entry : replacements) {
2516 std::optional<unsigned> index = getParamIndex(oldParamOrder, entry.first);
2517 if (!index || !callParams || *index >= callParams.size()) {
2520 Attribute attr = callParams[*index];
2521 Type expectedTy = substituteExplicitCallTypeArgs(entry.second, callParams, oldParamOrder);
2522 if (
auto intAttr = llvm::dyn_cast<IntegerAttr>(attr);
2523 intAttr &&
isDynamic(intAttr) && wildcardAllowedReplacements &&
2524 wildcardAllowedReplacements->contains(entry.first)) {
2527 if (templateArgUnifiesWithType(attr, expectedTy)) {
2531 InFlightDiagnostic diag = callableOp->emitError()
2532 << (explicitCallParams ?
"explicit" :
"implicit")
2533 <<
" template argument for inferred parameter @"
2534 << entry.first.getValue() <<
" must match inferred type "
2535 << expectedTy <<
", but found ";
2536 if (
auto typeAttr = llvm::dyn_cast<TypeAttr>(attr)) {
2537 diag << typeAttr.getValue();
2543 return callableOp->emitError() << (explicitCallParams ?
"explicit" :
"implicit")
2544 <<
" template arguments do not match inferred callee types";
2550static ArrayAttr expandCurrentTemplateParamsToOriginalOrder(
2551 ArrayAttr callParams,
const TemplateInferenceInfo &info
2553 if (!callParams || callParams.size() == info.oldParamOrder.size()) {
2557 unsigned currentParamCount = llvm::count_if(info.oldParamOrder, [&info](StringAttr paramName) {
2558 return !info.replacements.contains(paramName);
2560 if (callParams.size() != currentParamCount) {
2564 SmallVector<Attribute> expandedParams;
2565 expandedParams.reserve(info.oldParamOrder.size());
2566 unsigned currentIndex = 0;
2567 for (StringAttr paramName : info.oldParamOrder) {
2568 auto replacementIt = info.replacements.find(paramName);
2569 if (replacementIt != info.replacements.end()) {
2570 expandedParams.push_back(TypeAttr::get(replacementIt->second.type));
2573 expandedParams.push_back(callParams[currentIndex++]);
2575 return ArrayAttr::get(callParams.getContext(), expandedParams);
2584template <
typename TargetOp>
2585static DenseMap<StringAttr, Type> getConcreteCallSiteReplacements(
2586 ArrayAttr callParams,
const TemplateInferenceInfo &info, TargetOp targetOp
2588 DenseMap<StringAttr, Type> replacements;
2589 if (!callParams || callParams.size() != info.oldParamOrder.size()) {
2590 return replacements;
2593 bool sawResidualReplacement =
false;
2594 for (
const auto &entry : info.typeVarParams) {
2595 StringAttr paramName = entry.first;
2596 if (info.replacements.contains(paramName)) {
2599 if (!targetMentionsParam(targetOp, paramName)) {
2602 std::optional<unsigned> index = getParamIndex(info.oldParamOrder, paramName);
2603 assert(index &&
"eligible type-variable parameter must appear in parameter order");
2604 Attribute attr = callParams[*index];
2605 auto tyAttr = llvm::dyn_cast<TypeAttr>(attr);
2607 return DenseMap<StringAttr, Type>();
2609 replacements.try_emplace(paramName, tyAttr.getValue());
2610 sawResidualReplacement =
true;
2612 if (!sawResidualReplacement) {
2613 return DenseMap<StringAttr, Type>();
2616 for (
const auto &entry : info.replacements) {
2617 replacements.try_emplace(entry.first, entry.second.type);
2620 TypeVarReplacementConverter converter(
2621 info.templatePath.front().getContext(), info.templatePath, info.oldParamOrder, replacements,
2624 for (
const auto &entry : replacements) {
2626 return DenseMap<StringAttr, Type>();
2629 return replacements;
2633static bool isWildcardTemplateArg(Attribute attr) {
2634 auto intAttr = llvm::dyn_cast_if_present<IntegerAttr>(attr);
2639static void appendUniqueType(SmallVectorImpl<Type> &types, Type ty) {
2640 if (!llvm::any_of(types, [ty](Type existing) {
return existing == ty; })) {
2641 types.push_back(ty);
2646static void collectRhsTypeVarCandidates(
2647 Type lhsTy, Type rhsTy, SymbolRefAttr targetRef, SmallVectorImpl<Type> &candidates
2651static void collectRhsTypeVarCandidates(
2652 ArrayRef<Attribute> lhsAttrs, ArrayRef<Attribute> rhsAttrs, SymbolRefAttr targetRef,
2653 SmallVectorImpl<Type> &candidates
2655 if (lhsAttrs.size() != rhsAttrs.size()) {
2658 for (
auto [lhsAttr, rhsAttr] : llvm::zip_equal(lhsAttrs, rhsAttrs)) {
2659 collectRhsTypeVarCandidates(lhsAttr, rhsAttr, targetRef, candidates);
2664static void collectRhsTypeVarCandidates(
2665 Attribute lhsAttr, Attribute rhsAttr, SymbolRefAttr targetRef, SmallVectorImpl<Type> &candidates
2667 if (
auto rhsTypeAttr = llvm::dyn_cast_if_present<TypeAttr>(rhsAttr)) {
2668 if (
auto lhsTypeAttr = llvm::dyn_cast_if_present<TypeAttr>(lhsAttr)) {
2669 collectRhsTypeVarCandidates(
2670 lhsTypeAttr.getValue(), rhsTypeAttr.getValue(), targetRef, candidates
2676 auto lhsArray = llvm::dyn_cast_if_present<ArrayAttr>(lhsAttr);
2677 auto rhsArray = llvm::dyn_cast_if_present<ArrayAttr>(rhsAttr);
2678 if (!lhsArray || !rhsArray || lhsArray.size() != rhsArray.size()) {
2681 collectRhsTypeVarCandidates(lhsArray.getValue(), rhsArray.getValue(), targetRef, candidates);
2689static void collectRhsTypeVarCandidates(
2690 Type lhsTy, Type rhsTy, SymbolRefAttr targetRef, SmallVectorImpl<Type> &candidates
2692 if (
auto rhsTvar = llvm::dyn_cast<TypeVarType>(rhsTy);
2693 rhsTvar && rhsTvar.getNameRef() == targetRef) {
2694 appendUniqueType(candidates, lhsTy);
2698 if (
auto lhsArray = llvm::dyn_cast<ArrayType>(lhsTy)) {
2699 if (
auto rhsArray = llvm::dyn_cast<ArrayType>(rhsTy)) {
2700 collectRhsTypeVarCandidates(
2701 lhsArray.getElementType(), rhsArray.getElementType(), targetRef, candidates
2703 collectRhsTypeVarCandidates(
2704 lhsArray.getDimensionSizes(), rhsArray.getDimensionSizes(), targetRef, candidates
2710 if (
auto lhsStruct = llvm::dyn_cast<StructType>(lhsTy)) {
2711 if (
auto rhsStruct = llvm::dyn_cast<StructType>(rhsTy)) {
2712 collectRhsTypeVarCandidates(
2713 lhsStruct.getParams(), rhsStruct.getParams(), targetRef, candidates
2719 if (
auto lhsPod = llvm::dyn_cast<PodType>(lhsTy)) {
2720 if (
auto rhsPod = llvm::dyn_cast<PodType>(rhsTy);
2721 rhsPod && lhsPod.getRecords().size() == rhsPod.getRecords().size()) {
2722 for (
auto [lhsRecord, rhsRecord] :
2723 llvm::zip_equal(lhsPod.getRecords(), rhsPod.getRecords())) {
2724 collectRhsTypeVarCandidates(
2725 lhsRecord.getType(), rhsRecord.getType(), targetRef, candidates
2732 if (
auto lhsFunc = llvm::dyn_cast<FunctionType>(lhsTy)) {
2733 if (
auto rhsFunc = llvm::dyn_cast<FunctionType>(rhsTy)) {
2734 for (
auto [lhsInput, rhsInput] : llvm::zip_equal(lhsFunc.getInputs(), rhsFunc.getInputs())) {
2735 collectRhsTypeVarCandidates(lhsInput, rhsInput, targetRef, candidates);
2737 for (
auto [lhsResult, rhsResult] :
2738 llvm::zip_equal(lhsFunc.getResults(), rhsFunc.getResults())) {
2739 collectRhsTypeVarCandidates(lhsResult, rhsResult, targetRef, candidates);
2746static SmallVector<Type>
2747getConflictingRhsTypeVarCandidates(FunctionType lhs, FunctionType rhs, SymbolRefAttr targetRef) {
2748 SmallVector<Type> candidates;
2749 if (lhs.getNumInputs() != rhs.getNumInputs() || lhs.getNumResults() != rhs.getNumResults()) {
2752 for (
auto [lhsInput, rhsInput] : llvm::zip_equal(lhs.getInputs(), rhs.getInputs())) {
2753 collectRhsTypeVarCandidates(lhsInput, rhsInput, targetRef, candidates);
2755 for (
auto [lhsResult, rhsResult] : llvm::zip_equal(lhs.getResults(), rhs.getResults())) {
2756 collectRhsTypeVarCandidates(lhsResult, rhsResult, targetRef, candidates);
2762template <
typename CallableOp,
typename TargetOp>
2764emitConflictingInferredTypes(CallableOp callableOp, TargetOp targetOp, StringAttr paramName) {
2765 InFlightDiagnostic diag = callableOp.emitError()
2766 <<
"conflicting inferred types for @" << paramName.getValue();
2767 SmallVector<Type> candidates = getConflictingRhsTypeVarCandidates(
2768 callableOp.getTypeSignature(), targetOp.getFunctionType(), FlatSymbolRefAttr::get(paramName)
2770 if (candidates.size() >= 2) {
2771 diag <<
": " << candidates.front();
2772 for (Type candidate : llvm::drop_begin(candidates)) {
2773 diag <<
" vs " << candidate;
2780template <
typename CallableOp,
typename TargetOp>
2781static FailureOr<Attribute> getInferredRhsTemplateArg(
2782 CallableOp callableOp, TargetOp targetOp,
const UnificationMap &unifyResult,
2783 StringAttr paramName, StringRef argKind
2785 auto inferredIt = unifyResult.find({FlatSymbolRefAttr::get(paramName), Side::RHS});
2786 if (inferredIt == unifyResult.end()) {
2787 return callableOp.emitError() <<
"could not infer " << argKind
2788 <<
" template argument for parameter @" << paramName.getValue()
2789 <<
" from callee signature";
2791 if (!inferredIt->second) {
2792 return emitConflictingInferredTypes(callableOp, targetOp, paramName);
2794 return inferredIt->second;
2798template <
typename CallableOp,
typename TargetOp>
2799static FailureOr<ArrayAttr> getCallSignatureTemplateParams(
2800 CallableOp callableOp,
const TemplateInferenceInfo &info, TargetOp targetOp
2802 FailureOr<UnificationMap> unifyResult = callableOp.unifyTypeSignature(targetOp.getFunctionType());
2803 if (failed(unifyResult)) {
2804 return callableOp.emitError()
2805 <<
"could not infer omitted template arguments from callee signature";
2808 SmallVector<Attribute> params;
2809 params.reserve(info.oldParamOrder.size());
2810 for (StringAttr paramName : info.oldParamOrder) {
2811 auto replacementIt = info.replacements.find(paramName);
2812 if (replacementIt != info.replacements.end()) {
2813 params.push_back(TypeAttr::get(replacementIt->second.type));
2816 FailureOr<Attribute> inferredAttr =
2817 getInferredRhsTemplateArg(callableOp, targetOp, *unifyResult, paramName,
"omitted");
2818 if (failed(inferredAttr)) {
2821 params.push_back(*inferredAttr);
2823 return ArrayAttr::get(callableOp.getContext(), params);
2828template <
typename TargetOp>
2829static bool hasResidualFunctionTvarWildcard(
2830 ArrayAttr callParams,
const TemplateInferenceInfo &info, TargetOp targetOp
2832 if (!callParams || callParams.size() != info.oldParamOrder.size()) {
2835 for (
const auto &entry : info.typeVarParams) {
2836 StringAttr paramName = entry.first;
2837 if (info.replacements.contains(paramName)) {
2840 if (!targetMentionsParam(targetOp, paramName)) {
2843 std::optional<unsigned> index = getParamIndex(info.oldParamOrder, paramName);
2844 assert(index &&
"eligible type-variable parameter must appear in parameter order");
2845 if (isWildcardTemplateArg(callParams[*index])) {
2854template <
typename CallableOp,
typename TargetOp>
2855static FailureOr<ArrayAttr> materializeResidualFunctionTvarWildcards(
2856 CallableOp callableOp, ArrayAttr callParams,
const TemplateInferenceInfo &info,
2859 FailureOr<UnificationMap> unifyResult = callableOp.unifyTypeSignature(targetOp.getFunctionType());
2860 if (failed(unifyResult)) {
2861 return callableOp.emitError()
2862 <<
"could not infer wildcard template arguments from callee signature";
2865 SmallVector<Attribute> params(callParams.begin(), callParams.end());
2866 for (
const auto &entry : info.typeVarParams) {
2867 StringAttr paramName = entry.first;
2868 if (info.replacements.contains(paramName)) {
2871 if (!targetMentionsParam(targetOp, paramName)) {
2874 std::optional<unsigned> index = getParamIndex(info.oldParamOrder, paramName);
2875 assert(index &&
"eligible type-variable parameter must appear in parameter order");
2876 if (!isWildcardTemplateArg(params[*index])) {
2880 auto inferredIt = unifyResult->find({FlatSymbolRefAttr::get(paramName), Side::RHS});
2881 if (inferredIt == unifyResult->end()) {
2882 auto proofIt = info.functionReplacements.find(targetOp.getOperation());
2883 if (proofIt == info.functionReplacements.end()) {
2886 auto replacementIt = proofIt->second.find(paramName);
2887 if (replacementIt == proofIt->second.end()) {
2890 params[*index] = TypeAttr::get(replacementIt->second.type);
2893 if (!inferredIt->second) {
2894 return emitConflictingInferredTypes(callableOp, targetOp, paramName);
2896 params[*index] = inferredIt->second;
2898 return ArrayAttr::get(callableOp.getContext(), params);
2906template <
typename TargetOp>
2907static bool hasResidualFunctionTvar(
const TemplateInferenceInfo &info, TargetOp targetOp) {
2908 return llvm::any_of(info.typeVarParams, [&](
const auto &entry) {
2909 return !info.replacements.contains(entry.first) && targetMentionsParam(targetOp, entry.first);
2914struct CallableSpecializationInputs {
2916 bool explicitParams =
false;
2917 bool paramsChanged =
false;
2918 DenseMap<StringAttr, Type> replacements;
2922template <
typename CallableOp,
typename TargetOp>
2923static LogicalResult prepareCallableSpecialization(
2924 CallableOp callableOp,
const TemplateInferenceInfo &info, TargetOp targetOp,
2925 CallableSpecializationInputs &inputs,
bool &shouldSpecialize
2927 shouldSpecialize =
false;
2929 inputs.explicitParams =
false;
2930 inputs.paramsChanged =
false;
2931 inputs.replacements.clear();
2932 inputs.params = callableOp.getTemplateParamsAttr();
2935 FailureOr<ArrayAttr> inferredParams =
2936 getCallSignatureTemplateParams(callableOp, info, targetOp);
2937 if (failed(inferredParams)) {
2940 inputs.params = *inferredParams;
2941 inputs.paramsChanged =
true;
2943 inputs.params = expandCurrentTemplateParamsToOriginalOrder(inputs.params, info);
2944 if (hasResidualFunctionTvarWildcard(inputs.params, info, targetOp)) {
2945 FailureOr<ArrayAttr> materializedParams =
2946 materializeResidualFunctionTvarWildcards(callableOp, inputs.params, info, targetOp);
2947 if (failed(materializedParams)) {
2950 inputs.params = *materializedParams;
2951 inputs.paramsChanged =
true;
2955 inputs.replacements = getConcreteCallSiteReplacements(inputs.params, info, targetOp);
2956 shouldSpecialize = !inputs.replacements.empty();
2966static std::string buildTemplateLocalFunctionCloneCacheKey(
2967 StringRef functionName, ArrayRef<StringAttr> oldParamOrder,
2968 const DenseMap<StringAttr, Type> &replacements
2971 llvm::raw_string_ostream os(key);
2972 os << functionName.size() <<
':' << functionName;
2973 for (StringAttr paramName : oldParamOrder) {
2975 os << paramName.getValue().size() <<
':' << paramName.getValue() <<
'=';
2976 auto replacementIt = replacements.find(paramName);
2977 if (replacementIt == replacements.end()) {
2982 std::string typeText;
2983 llvm::raw_string_ostream typeOs(typeText);
2984 replacementIt->second.print(typeOs);
2985 os << typeText.size() <<
':' << typeText;
2991static SymbolRefAttr getSpecializedFunctionCloneCallee(
2992 SymbolRefAttr originalCallee, StringAttr templateName, StringAttr cloneName
2994 SmallVector<FlatSymbolRefAttr> pieces =
getPieces(originalCallee);
2995 assert(pieces.size() >= 2 &&
"callee must include at least template and function names");
2998 pieces.push_back(FlatSymbolRefAttr::get(templateName));
2999 pieces.push_back(FlatSymbolRefAttr::get(cloneName));
3004template <
typename CallableOp,
typename ConfigureCloneFn>
3005static FailureOr<SymbolRefAttr> getOrCreateSpecializedCallableClone(
3006 TemplateOp templateOp, CallableOp callable, SymbolRefAttr originalCallee,
3007 const TemplateInferenceInfo &info,
const DenseMap<StringAttr, Type> &replacements,
3008 SymbolTableCollection &tables, llvm::StringMap<SymbolRefAttr> &cloneCallees,
3009 llvm::StringMap<StringAttr> &templateClones, InstantiationLayout &layout, ArrayAttr callParams,
3010 ConfigureCloneFn configureClone
3012 DenseMap<Attribute, Attribute> paramNameToConcrete = buildParamNameToConcrete(replacements);
3013 FailureOr<TemplateOp> newTemplate = getOrCreateSpecializedTemplateClone(
3014 templateOp, info.oldParamOrder, paramNameToConcrete, callParams, tables, templateClones,
3017 if (failed(newTemplate)) {
3021 std::string cacheKey = buildTemplateLocalFunctionCloneCacheKey(
3022 callable.getSymName(), info.oldParamOrder, replacements
3024 auto cachedCallee = cloneCallees.find(cacheKey);
3025 if (cachedCallee != cloneCallees.end()) {
3026 return cachedCallee->second;
3029 SymbolTable &templateSymbols = tables.getSymbolTable(*newTemplate);
3031 auto clone = llvm::cast<CallableOp>(callable.getOperation()->clone());
3032 configureClone(clone);
3033 templateSymbols.insert(clone);
3035 TypeVarReplacementConverter converter(
3036 templateOp.getContext(), info.templatePath, info.oldParamOrder, replacements,
3039 if (failed(convertTemplateExprTypesIn(*newTemplate, converter))) {
3043 if (failed(convertOperationTypesIn(clone.getOperation(), converter))) {
3047 removeIdentityCasts(clone.getOperation());
3048 SymbolRefAttr cloneCallee = getSpecializedFunctionCloneCallee(
3049 originalCallee, newTemplate->getSymNameAttr(), clone.
getSymNameAttr()
3051 cloneCallees.try_emplace(cacheKey, cloneCallee);
3056static FailureOr<SymbolRefAttr> getOrCreateSpecializedFunctionClone(
3057 TemplateOp templateOp, FuncDefOp func, SymbolRefAttr originalCallee,
3058 const TemplateInferenceInfo &info,
const DenseMap<StringAttr, Type> &replacements,
3059 SymbolTableCollection &tables, llvm::StringMap<SymbolRefAttr> &cloneCallees,
3060 llvm::StringMap<StringAttr> &templateClones, InstantiationLayout &layout, ArrayAttr callParams
3062 return getOrCreateSpecializedCallableClone(
3063 templateOp, func, originalCallee, info, replacements, tables, cloneCallees, templateClones,
3064 layout, callParams, [](FuncDefOp) {}
3069static FailureOr<SymbolRefAttr> getOrCreateSpecializedContractClone(
3070 TemplateOp templateOp, verif::ContractOp contract, SymbolRefAttr originalCallee,
3071 SymbolRefAttr specializedTarget,
const TemplateInferenceInfo &info,
3072 const DenseMap<StringAttr, Type> &replacements, SymbolTableCollection &tables,
3073 llvm::StringMap<SymbolRefAttr> &cloneCallees, llvm::StringMap<StringAttr> &templateClones,
3074 InstantiationLayout &layout, ArrayAttr callParams
3076 return getOrCreateSpecializedCallableClone(
3077 templateOp, contract, originalCallee, info, replacements, tables, cloneCallees,
3078 templateClones, layout, callParams,
3079 [specializedTarget](verif::ContractOp clone) { clone.
setTargetAttr(specializedTarget); }
3087static void removeResolvedParams(TemplateInferenceInfo &info) {
3088 for (
auto paramOp : llvm::make_early_inc_range(info.templateOp.
getConstOps<TemplateParamOp>())) {
3089 auto name = paramOp.getSymNameAttr();
3090 if (info.replacements.contains(name)) {
3097template <
typename CallableOp>
3099validateWildcardCallableSignature(CallableOp callableOp, FunctionType targetTy) {
3100 if (succeeded(callableOp.unifyTypeSignature(targetTy))) {
3103 return callableOp.emitError() <<
"call signature " << callableOp.getTypeSignature()
3104 <<
" does not match inferred callee signature " << targetTy;
3108static void updateCallableResultTypesIfNeeded(
3109 CallOp callOp,
const TypeVarReplacementConverter &converter,
bool &modified
3111 for (Value result : callOp.getResults()) {
3112 Type newTy = converter.convertType(result.getType());
3113 if (newTy != result.getType()) {
3114 result.setType(newTy);
3122updateCallableResultTypesIfNeeded(verif::IncludeOp,
const TypeVarReplacementConverter &,
bool &) {}
3125template <
typename CallableOp>
3126static FailureOr<bool> updateCallableTemplateParamsFor(
3127 ModuleOp module, DenseMap<Operation *, const TypeVarReplacementConverter *> &converters,
3128 SymbolTableCollection &tables
3130 bool modified =
false;
3131 bool failedConversion =
false;
3132 module.walk([&](CallableOp callableOp) {
3133 if (failedConversion) {
3136 auto target = callableOp.getCalleeTarget(tables);
3137 if (failed(target)) {
3140 auto targetOp = target->get();
3142 if (!parentTemplate) {
3145 const TypeVarReplacementConverter *converter = converters.lookup(parentTemplate.getOperation());
3151 bool resolveTemplateSymbolArgs =
3152 callableTemplate && callableTemplate.getOperation() == parentTemplate.getOperation();
3153 ArrayAttr oldParams = callableOp.getTemplateParamsAttr();
3154 FailureOr<ArrayAttr> newParams = converter->convertTemplateParams(
3155 oldParams, callableOp, resolveTemplateSymbolArgs,
3158 if (failed(newParams)) {
3159 failedConversion =
true;
3162 if (converter->hasWildcardForRemovedParam(oldParams)) {
3163 auto inferredTargetTy =
3164 llvm::cast<FunctionType>(converter->convertType(targetOp.getFunctionType()));
3165 if (failed(validateWildcardCallableSignature(callableOp, inferredTargetTy))) {
3166 failedConversion =
true;
3170 if (oldParams != *newParams) {
3171 callableOp.setTemplateParamsAttr(*newParams);
3175 updateCallableResultTypesIfNeeded(callableOp, *converter, modified);
3177 if (failedConversion) {
3190static FailureOr<bool> updateCallableTemplateParams(
3191 ModuleOp module, DenseMap<Operation *, const TypeVarReplacementConverter *> &converters
3193 SymbolTableCollection tables;
3194 FailureOr<bool> callsModified =
3195 updateCallableTemplateParamsFor<CallOp>(module, converters, tables);
3196 if (failed(callsModified)) {
3199 FailureOr<bool> includesModified =
3200 updateCallableTemplateParamsFor<verif::IncludeOp>(module, converters, tables);
3201 if (failed(includesModified)) {
3204 return *callsModified || *includesModified;
3213class ReferencedStructTemplateParamConverter {
3219 SymbolTableCollection &tables_;
3221 DenseMap<Operation *, const TypeVarReplacementConverter *> &converters_;
3223 Operation *diagnosticOp_ =
nullptr;
3225 bool hasFailure =
false;
3228 ReferencedStructTemplateParamConverter(
3229 MLIRContext *ctx, ModuleOp module, SymbolTableCollection &tables,
3230 DenseMap<Operation *, const TypeVarReplacementConverter *> &converters
3232 : ctx_(ctx), module_(module), tables_(tables), converters_(converters) {}
3235 void startOperation(Operation *op) {
3241 bool hadFailure()
const {
return hasFailure; }
3244 Type convertType(Type ty) {
3245 if (!ty || hasFailure) {
3248 if (
auto arrTy = llvm::dyn_cast<ArrayType>(ty)) {
3249 return convertArrayElementType(arrTy, [
this](Type elemTy) {
return convertType(elemTy); });
3251 if (
auto structTy = llvm::dyn_cast<StructType>(ty)) {
3252 return convertStructType(structTy);
3254 if (
auto podTy = llvm::dyn_cast<PodType>(ty)) {
3255 return convertPodType(podTy, ctx_, *
this);
3257 if (
auto funcTy = llvm::dyn_cast<FunctionType>(ty)) {
3258 return convertFunctionType(funcTy, *
this);
3264 Attribute convertAttr(Attribute attr) {
3268 return convertTypeOrArrayAttr(attr, ctx_, [
this](Type ty) {
3269 return convertType(ty);
3270 }, [
this](Attribute nested) {
return convertAttr(nested); });
3275 StructType convertStructType(StructType structTy) {
3276 ArrayAttr params = structTy.
getParams();
3281 SmallVector<Attribute> newParams;
3282 bool changed =
false;
3283 for (Attribute attr : params.getValue()) {
3284 Attribute newAttr = convertAttr(attr);
3285 newParams.push_back(newAttr);
3286 changed |= newAttr != attr;
3291 StructType convertedTy =
3295 tables_, convertedTy.
getNameRef(), module_,
false
3297 if (failed(lookup)) {
3301 if (!parentTemplate) {
3304 const TypeVarReplacementConverter *converter =
3305 converters_.lookup(parentTemplate.getOperation());
3311 bool resolveTemplateSymbolArgs =
3312 useTemplate && useTemplate.getOperation() == parentTemplate.getOperation();
3313 FailureOr<ArrayAttr> trimmedParams = converter->convertTemplateParams(
3314 convertedTy.
getParams(), diagnosticOp_, resolveTemplateSymbolArgs,
3317 if (failed(trimmedParams)) {
3321 if (convertedTy.
getParams() == *trimmedParams) {
3329static FailureOr<bool> updateStructTemplateParams(
3330 ModuleOp module, DenseMap<Operation *, const TypeVarReplacementConverter *> &converters
3332 SymbolTableCollection tables;
3333 ReferencedStructTemplateParamConverter converter(module.getContext(), module, tables, converters);
3334 return convertOperationTypesInAndTrack(module.getOperation(), converter);
3338static LogicalResult instantiateConcreteStructUses(ModuleOp module) {
3339 SymbolTableCollection tables;
3340 ConcreteStructInstantiationConverter converter(module.getContext(), module, tables);
3341 return convertOperationTypesIn(module.getOperation(), converter);
3346getContractTargetTemplate(verif::ContractOp contract, SymbolTableCollection &tables) {
3347 if (FailureOr<SymbolLookupResult<FuncDefOp>> funcTarget = contract.
getFuncTarget(tables);
3348 succeeded(funcTarget)) {
3351 if (FailureOr<SymbolLookupResult<StructDefOp>> structTarget = contract.
getStructTarget(tables);
3352 succeeded(structTarget)) {
3364static LogicalResult updateExternalContractTemplateParams(
3365 ModuleOp module, DenseMap<Operation *, const TypeVarReplacementConverter *> &converters
3367 bool failedConversion =
false;
3368 SymbolTableCollection tables;
3369 module.walk([&](verif::ContractOp contract) {
3370 if (failedConversion) {
3373 TemplateOp targetTemplate = getContractTargetTemplate(contract, tables);
3374 if (!targetTemplate) {
3380 const TypeVarReplacementConverter *converter = converters.lookup(targetTemplate.getOperation());
3385 WalkResult validationResult = contract.walk([converter](Operation *op) {
3386 return failed(converter->validateOperation(op)) ? WalkResult::interrupt()
3387 : WalkResult::advance();
3389 if (validationResult.wasInterrupted() ||
3390 failed(convertOperationTypesIn(contract.getOperation(), *converter))) {
3391 failedConversion =
true;
3394 removeIdentityCasts(contract.getOperation());
3396 return failure(failedConversion);
3407static LogicalResult specializeFunctionLocalCallables(
3408 ModuleOp module, DenseMap<Operation *, const TemplateInferenceInfo *> &infoByTemplate,
3409 SpecializedCallableCloneCache &functionCloneCache,
3410 SpecializedCallableCloneCache &contractCloneCache,
3411 SpecializedTemplateCloneCache &templateCloneCache
3413 bool failedClone =
false;
3414 SymbolTableCollection tables;
3415 module.walk([&](CallOp callOp) {
3419 FailureOr<SymbolLookupResult<FuncDefOp>> target = callOp.
getCalleeTarget(tables);
3420 if (failed(target)) {
3423 FuncDefOp targetFunc = target->get();
3424 auto parentTemplate = llvm::dyn_cast_or_null<TemplateOp>(targetFunc->getParentOp());
3425 if (!parentTemplate) {
3428 const TemplateInferenceInfo *info = infoByTemplate.lookup(parentTemplate.getOperation());
3432 if (!hasResidualFunctionTvar(*info, targetFunc)) {
3436 CallableSpecializationInputs inputs;
3437 bool shouldSpecialize =
false;
3439 prepareCallableSpecialization(callOp, *info, targetFunc, inputs, shouldSpecialize)
3444 if (!shouldSpecialize) {
3447 DenseMap<StringAttr, Type> inferredReplacements =
3448 getFunctionProofReplacements(*info, targetFunc);
3449 if (failed(diagnoseCallParamsMismatch(
3450 callOp.getOperation(), inputs.params, info->oldParamOrder, inferredReplacements,
3451 &info->replacements, inputs.explicitParams
3457 InstantiationLayout layout;
3458 FailureOr<SymbolRefAttr> cloneCallee = getOrCreateSpecializedFunctionClone(
3459 parentTemplate, targetFunc, callOp.
getCalleeAttr(), *info, inputs.replacements, tables,
3460 functionCloneCache[parentTemplate.getOperation()],
3461 templateCloneCache[parentTemplate.getOperation()], layout, inputs.params
3463 if (failed(cloneCallee)) {
3471 module.walk([&](verif::IncludeOp includeOp) {
3475 FailureOr<SymbolLookupResult<verif::ContractOp>> target = includeOp.
getCalleeTarget(tables);
3476 if (failed(target)) {
3479 verif::ContractOp targetContract = target->get();
3480 auto parentTemplate = llvm::dyn_cast_or_null<TemplateOp>(targetContract->getParentOp());
3481 if (!parentTemplate) {
3484 const TemplateInferenceInfo *info = infoByTemplate.lookup(parentTemplate.getOperation());
3488 if (!hasResidualFunctionTvar(*info, targetContract)) {
3492 FailureOr<SymbolLookupResult<FuncDefOp>> targetFuncResult =
3494 if (failed(targetFuncResult)) {
3497 FuncDefOp targetFunc = targetFuncResult->get();
3502 CallableSpecializationInputs inputs;
3503 bool shouldSpecialize =
false;
3504 if (failed(prepareCallableSpecialization(
3505 includeOp, *info, targetContract, inputs, shouldSpecialize
3510 if (!shouldSpecialize) {
3513 DenseMap<StringAttr, Type> inferredReplacements =
3514 getFunctionProofReplacements(*info, targetFunc);
3515 if (failed(diagnoseCallParamsMismatch(
3516 includeOp.getOperation(), inputs.params, info->oldParamOrder, inferredReplacements,
3517 &info->replacements, inputs.explicitParams
3523 InstantiationLayout targetLayout;
3524 FailureOr<SymbolRefAttr> specializedTarget = getOrCreateSpecializedFunctionClone(
3525 parentTemplate, targetFunc, targetContract.
getTargetAttr(), *info, inputs.replacements,
3526 tables, functionCloneCache[parentTemplate.getOperation()],
3527 templateCloneCache[parentTemplate.getOperation()], targetLayout, inputs.params
3529 if (failed(specializedTarget)) {
3533 InstantiationLayout contractLayout;
3534 FailureOr<SymbolRefAttr> cloneCallee = getOrCreateSpecializedContractClone(
3535 parentTemplate, targetContract, includeOp.
getCalleeAttr(), *specializedTarget, *info,
3536 inputs.replacements, tables, contractCloneCache[parentTemplate.getOperation()],
3537 templateCloneCache[parentTemplate.getOperation()], contractLayout, inputs.params
3539 if (failed(cloneCallee)) {
3547 return failure(failedClone);
3551static bool sameReplacementTypes(
3552 const DenseMap<StringAttr, InferredType> &lhs,
const DenseMap<StringAttr, InferredType> &rhs
3554 if (lhs.size() != rhs.size()) {
3557 for (
const auto &entry : lhs) {
3558 auto rhsIt = rhs.find(entry.first);
3559 if (rhsIt == rhs.end() || rhsIt->second.type != entry.second.type) {
3572static LogicalResult collectContractTargetFunctionInferences(
3573 TemplateInferenceInfo &info, verif::ContractOp contract, ModuleOp module,
3574 DenseMap<Operation *, TemplateInferenceInfo *> *infoByTemplate, SymbolTableCollection &tables,
3577 FailureOr<SymbolLookupResult<FuncDefOp>> target = contract.
getFuncTarget(tables);
3578 if (failed(target)) {
3581 FuncDefOp targetFunc = target->get();
3586 DenseMap<StringAttr, InferredType> funcReplacements;
3587 auto funcIt = info.functionReplacements.find(targetFunc.getOperation());
3588 if (funcIt != info.functionReplacements.end()) {
3589 funcReplacements = funcIt->second;
3591 DenseMap<StringAttr, InferredType> oldFuncReplacements = funcReplacements;
3593 TypeVarInferenceCollector collector(info, funcReplacements);
3594 if (failed(collector.collect(contract.getOperation()))) {
3597 if (infoByTemplate && failed(collector.collectStructTemplateParamInferences(
3598 contract.getOperation(), module, *infoByTemplate, tables
3603 if (!sameReplacementTypes(oldFuncReplacements, funcReplacements)) {
3606 if (funcReplacements.empty()) {
3607 info.functionReplacements.erase(targetFunc.getOperation());
3609 info.functionReplacements[targetFunc.getOperation()] = std::move(funcReplacements);
3620static LogicalResult collectContractTargetStructInferences(
3621 TemplateInferenceInfo &info, verif::ContractOp contract, ModuleOp module,
3622 DenseMap<Operation *, TemplateInferenceInfo *> *infoByTemplate, SymbolTableCollection &tables,
3625 FailureOr<SymbolLookupResult<StructDefOp>> target = contract.
getStructTarget(tables);
3626 if (failed(target)) {
3629 StructDefOp targetStruct = target->get();
3634 DenseMap<StringAttr, InferredType> oldTemplateScopeReplacements = info.templateScopeReplacements;
3636 TypeVarInferenceCollector collector(info, info.templateScopeReplacements);
3638 if (contractTy.getNumInputs() > 0) {
3639 StructType targetSelfTy = targetStruct.
getType();
3640 if (
auto contractSelfTy = llvm::dyn_cast<StructType>(contractTy.getInput(0));
3641 contractSelfTy && contractSelfTy.getNameRef() == targetSelfTy.
getNameRef() &&
3642 failed(collector.collectTypeInferences(contractSelfTy, targetSelfTy, contract.getLoc()))) {
3646 if (failed(collector.collect(contract.getOperation()))) {
3649 if (infoByTemplate && failed(collector.collectStructTemplateParamInferences(
3650 contract.getOperation(), module, *infoByTemplate, tables
3655 if (!sameReplacementTypes(oldTemplateScopeReplacements, info.templateScopeReplacements)) {
3670static LogicalResult recomputeTemplateWideReplacements(TemplateInferenceInfo &info) {
3671 DenseMap<StringAttr, unsigned> mentionCounts;
3672 DenseMap<StringAttr, unsigned> proofCounts;
3673 DenseMap<StringAttr, InferredType> commonReplacements;
3674 DenseSet<StringAttr> incompatibleReplacements;
3676 for (FuncDefOp func : walkCollect<FuncDefOp>(info.templateOp)) {
3677 for (
const auto &entry : info.typeVarParams) {
3678 if (funcMentionsParam(func, entry.first)) {
3679 ++mentionCounts[entry.first];
3683 auto funcIt = info.functionReplacements.find(func.getOperation());
3684 if (funcIt == info.functionReplacements.end()) {
3687 for (
const auto &entry : funcIt->second) {
3688 ++proofCounts[entry.first];
3689 auto commonIt = commonReplacements.find(entry.first);
3690 if (commonIt == commonReplacements.end()) {
3691 commonReplacements.try_emplace(entry.first, entry.second);
3692 }
else if (commonIt->second.type != entry.second.type) {
3693 incompatibleReplacements.insert(entry.first);
3698 for (TemplateExprOp expr : walkCollect<TemplateExprOp>(info.templateOp)) {
3699 for (
const auto &entry : info.typeVarParams) {
3700 if (exprMentionsParam(expr, entry.first)) {
3701 ++mentionCounts[entry.first];
3706 info.replacements = info.templateScopeReplacements;
3707 for (
const auto &entry : info.templateScopeReplacements) {
3708 auto commonIt = commonReplacements.find(entry.first);
3709 if (commonIt != commonReplacements.end() && commonIt->second.type != entry.second.type) {
3710 InFlightDiagnostic diag = emitError(entry.second.loc)
3711 <<
"conflicting inferred type for template parameter @"
3712 << entry.first.getValue() <<
": " << commonIt->second.type <<
" vs "
3713 << entry.second.type;
3714 diag.attachNote(commonIt->second.loc) <<
"previous function-local inference here";
3719 for (
const auto &entry : commonReplacements) {
3720 StringAttr paramName = entry.first;
3721 if (info.replacements.contains(paramName)) {
3724 if (!hasUncoveredNonFunctionMention(
3725 info.templateOp, paramName, entry.second.type, info.functionReplacements
3727 !incompatibleReplacements.contains(paramName) &&
3728 proofCounts.lookup(paramName) == mentionCounts.lookup(paramName)) {
3729 info.replacements.try_emplace(paramName, entry.second);
3737static LogicalResult inferStructTemplateParamUses(
3738 ModuleOp module, MutableArrayRef<TemplateInferenceInfo> templateInfos,
3739 DenseMap<Operation *, TemplateInferenceInfo *> &infoByTemplate
3741 bool changed =
false;
3744 SymbolTableCollection tables;
3745 for (TemplateInferenceInfo &info : templateInfos) {
3746 for (FuncDefOp func : walkCollect<FuncDefOp>(info.templateOp)) {
3747 DenseMap<StringAttr, InferredType> funcReplacements;
3748 auto funcIt = info.functionReplacements.find(func.getOperation());
3749 if (funcIt != info.functionReplacements.end()) {
3750 funcReplacements = funcIt->second;
3752 DenseMap<StringAttr, InferredType> oldFuncReplacements = funcReplacements;
3754 TypeVarInferenceCollector collector(info, funcReplacements);
3755 if (failed(collector.collect(func.getOperation())) ||
3756 failed(collector.collectStructTemplateParamInferences(
3757 func.getOperation(), module, infoByTemplate, tables
3762 if (!sameReplacementTypes(oldFuncReplacements, funcReplacements)) {
3765 if (funcReplacements.empty()) {
3766 info.functionReplacements.erase(func.getOperation());
3768 info.functionReplacements[func.getOperation()] = std::move(funcReplacements);
3772 for (verif::ContractOp contract : walkCollect<verif::ContractOp>(info.templateOp)) {
3773 if (failed(collectContractTargetFunctionInferences(
3774 info, contract, module, &infoByTemplate, tables, changed
3776 failed(collectContractTargetStructInferences(
3777 info, contract, module, &infoByTemplate, tables, changed
3783 for (TemplateExprOp expr : walkCollect<TemplateExprOp>(info.templateOp)) {
3784 DenseMap<StringAttr, InferredType> oldTemplateScopeReplacements =
3785 info.templateScopeReplacements;
3787 TypeVarInferenceCollector collector(info, info.templateScopeReplacements);
3788 if (failed(collector.collect(expr.getOperation())) ||
3789 failed(collector.collectStructTemplateParamInferences(
3790 expr.getOperation(), module, infoByTemplate, tables
3795 if (!sameReplacementTypes(oldTemplateScopeReplacements, info.templateScopeReplacements)) {
3800 DenseMap<StringAttr, InferredType> oldTemplateScopeReplacements =
3801 info.templateScopeReplacements;
3802 TypeVarInferenceCollector collector(info, info.templateScopeReplacements);
3803 WalkResult nonFunctionResult = info.templateOp.walk([&](Operation *op) {
3804 if (llvm::isa<FuncDefOp, TemplateExprOp, verif::ContractOp>(op) ||
3806 return WalkResult::advance();
3808 if (failed(collector.collectOperationStructTemplateParamInferences(
3809 op, module, infoByTemplate, tables
3811 return WalkResult::interrupt();
3813 return WalkResult::advance();
3815 if (nonFunctionResult.wasInterrupted()) {
3818 if (!sameReplacementTypes(oldTemplateScopeReplacements, info.templateScopeReplacements)) {
3822 DenseMap<StringAttr, InferredType> oldReplacements = info.replacements;
3823 if (failed(recomputeTemplateWideReplacements(info))) {
3826 if (!sameReplacementTypes(oldReplacements, info.replacements)) {
3836static LogicalResult collectExternalContractTargetInferences(
3837 ModuleOp module, MutableArrayRef<TemplateInferenceInfo> templateInfos,
3838 DenseMap<Operation *, TemplateInferenceInfo *> &infoByTemplate
3840 bool changed =
false;
3843 bool failedCollection =
false;
3844 SymbolTableCollection tables;
3845 module.walk([&](verif::ContractOp contract) {
3846 TemplateOp targetTemplate = getContractTargetTemplate(contract, tables);
3847 if (!targetTemplate) {
3853 TemplateInferenceInfo *info = infoByTemplate.lookup(targetTemplate.getOperation());
3857 if (failed(collectContractTargetFunctionInferences(
3858 *info, contract, module, &infoByTemplate, tables, changed
3860 failed(collectContractTargetStructInferences(
3861 *info, contract, module, &infoByTemplate, tables, changed
3863 failedCollection =
true;
3866 if (failedCollection) {
3870 for (TemplateInferenceInfo &info : templateInfos) {
3871 DenseMap<StringAttr, InferredType> oldReplacements = info.replacements;
3872 if (failed(recomputeTemplateWideReplacements(info))) {
3875 if (!sameReplacementTypes(oldReplacements, info.replacements)) {
3890static FailureOr<TemplateInferenceInfo> buildInfo(TemplateOp templateOp) {
3891 TemplateInferenceInfo info;
3892 info.templateOp = templateOp;
3893 FailureOr<SymbolRefAttr> templatePath =
3894 getPathFromRoot(llvm::cast<SymbolOpInterface>(templateOp.getOperation()));
3895 if (failed(templatePath)) {
3898 info.templatePath = getStringPieces(*templatePath);
3899 for (TemplateParamOp paramOp : templateOp.
getConstOps<TemplateParamOp>()) {
3900 auto name = paramOp.getSymNameAttr();
3901 info.oldParamOrder.push_back(name);
3902 if (isTypeVarParam(paramOp)) {
3903 info.typeVarParams.try_emplace(name, paramOp);
3907 for (FuncDefOp func : walkCollect<FuncDefOp>(templateOp)) {
3908 DenseMap<StringAttr, InferredType> funcReplacements;
3909 if (failed(TypeVarInferenceCollector(info, funcReplacements).collect(func.getOperation()))) {
3912 if (!funcReplacements.empty()) {
3913 info.functionReplacements.try_emplace(func.getOperation(), funcReplacements);
3917 for (TemplateExprOp expr : walkCollect<TemplateExprOp>(templateOp)) {
3918 if (failed(TypeVarInferenceCollector(info, info.templateScopeReplacements)
3919 .collect(expr.getOperation()))) {
3924 bool changed =
false;
3925 SymbolTableCollection tables;
3926 ModuleOp module = templateOp->getParentOfType<ModuleOp>();
3927 for (verif::ContractOp contract : walkCollect<verif::ContractOp>(templateOp)) {
3928 if (failed(collectContractTargetFunctionInferences(
3929 info, contract, module,
nullptr, tables, changed
3931 failed(collectContractTargetStructInferences(
3932 info, contract, module,
nullptr, tables, changed
3938 if (failed(recomputeTemplateWideReplacements(info))) {
3951class PassImpl :
public llzk::polymorphic::impl::TypeVarInferencePassBase<PassImpl> {
3953 using Base = TypeVarInferencePassBase<PassImpl>;
3957 void runOnOperation()
override {
3958 ModuleOp module = getOperation();
3963 std::vector<TemplateInferenceInfo> templateInfos;
3964 WalkResult collectResult =
module.walk([&templateInfos](TemplateOp templateOp) {
3965 if (templateOp.getConstOps<TemplateParamOp>().empty()) {
3966 return WalkResult::advance();
3968 FailureOr<TemplateInferenceInfo> info = buildInfo(templateOp);
3970 return WalkResult::interrupt();
3972 templateInfos.push_back(std::move(*info));
3973 return WalkResult::advance();
3975 if (collectResult.wasInterrupted()) {
3976 signalPassFailure();
3979 if (templateInfos.empty()) {
3983 DenseMap<Operation *, TemplateInferenceInfo *> mutableInfoByTemplate;
3984 for (TemplateInferenceInfo &info : templateInfos) {
3985 mutableInfoByTemplate.try_emplace(info.templateOp.getOperation(), &info);
3988 collectExternalContractTargetInferences(module, templateInfos, mutableInfoByTemplate)
3990 signalPassFailure();
3993 if (failed(inferStructTemplateParamUses(module, templateInfos, mutableInfoByTemplate))) {
3994 signalPassFailure();
3998 SmallVector<TemplateInferenceInfo *> rewrites;
3999 DenseMap<Operation *, const TemplateInferenceInfo *> infoByTemplate;
4000 for (TemplateInferenceInfo &info : templateInfos) {
4001 infoByTemplate.try_emplace(info.templateOp.getOperation(), &info);
4002 if (!info.replacements.empty() || !info.functionReplacements.empty()) {
4003 rewrites.push_back(&info);
4009 SmallVector<std::unique_ptr<TypeVarReplacementConverter>> converterStorage;
4010 DenseMap<Operation *, const TypeVarReplacementConverter *> convertersByTemplate;
4011 for (TemplateInferenceInfo *info : rewrites) {
4012 DenseMap<StringAttr, Type> replacements;
4013 for (
const auto &entry : info->replacements) {
4014 replacements.try_emplace(entry.first, entry.second.type);
4016 auto converter = std::make_unique<TypeVarReplacementConverter>(
4017 module.getContext(), info->templatePath, info->oldParamOrder, replacements
4019 convertersByTemplate.try_emplace(info->templateOp.getOperation(), converter.get());
4020 converterStorage.push_back(std::move(converter));
4025 SpecializedCallableCloneCache functionCloneCache;
4026 SpecializedCallableCloneCache contractCloneCache;
4027 SpecializedTemplateCloneCache templateCloneCache;
4028 if (failed(specializeFunctionLocalCallables(
4029 module, infoByTemplate, functionCloneCache, contractCloneCache, templateCloneCache
4031 signalPassFailure();
4038 if (failed(updateCallableTemplateParams(module, convertersByTemplate))) {
4039 signalPassFailure();
4043 for (TemplateInferenceInfo *info : rewrites) {
4044 const TypeVarReplacementConverter *converter =
4045 convertersByTemplate.lookup(info->templateOp.getOperation());
4046 assert(converter &&
"rewritten template must have a converter");
4047 WalkResult validationResult = info->templateOp.walk([converter](Operation *op) {
4048 return failed(converter->validateOperation(op)) ? WalkResult::interrupt()
4049 : WalkResult::advance();
4051 if (validationResult.wasInterrupted()) {
4052 signalPassFailure();
4055 if (failed(convertOperationTypesIn(info->templateOp.getOperation(), *converter))) {
4056 signalPassFailure();
4059 removeIdentityCasts(info->templateOp.getOperation());
4062 if (failed(updateExternalContractTemplateParams(module, convertersByTemplate))) {
4063 signalPassFailure();
4069 if (failed(updateCallableTemplateParams(module, convertersByTemplate))) {
4070 signalPassFailure();
4075 if (failed(specializeFunctionLocalCallables(
4076 module, infoByTemplate, functionCloneCache, contractCloneCache, templateCloneCache
4078 signalPassFailure();
4081 if (failed(updateStructTemplateParams(module, convertersByTemplate))) {
4082 signalPassFailure();
4089 for (TemplateInferenceInfo *info : rewrites) {
4090 removeResolvedParams(*info);
4093 if (failed(instantiateConcreteStructUses(module))) {
4094 signalPassFailure();
Common private implementation for poly dialect passes.
This file defines methods symbol lookup across LLZK operations and included files.
Helper for converting between linear and multi-dimensional indexing with checks to ensure indices are...
static ArrayIndexGen from(ArrayType)
Construct new ArrayIndexGen. Will assert if hasStaticShape() is false.
ArrayType cloneWith(std::optional<::llvm::ArrayRef< int64_t > > shape, ::mlir::Type elementType) const
Clone this type with the given shape and element type.
::mlir::Type getElementType() const
::llvm::ArrayRef<::mlir::Attribute > getDimensionSizes() const
::mlir::TypedValue<::llzk::array::ArrayType > getResult()
::mlir::Operation::operand_range getElements()
StructType getType(::std::optional<::mlir::ArrayAttr > constParams={})
Gets the StructType representing this struct.
::mlir::SymbolRefAttr getFullyQualifiedName()
Return the full name for this struct from the root module, including any surrounding module scopes.
::mlir::StringAttr getSymNameAttr()
::mlir::SymbolRefAttr getNameRef() const
static StructType get(::mlir::SymbolRefAttr structName)
::mlir::FailureOr< SymbolLookupResult< StructDefOp > > getDefinition(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op, bool reportMissing=true) const
Gets the struct op that defines this struct.
::mlir::ArrayAttr getParams() const
::mlir::SymbolRefAttr getCalleeAttr()
::mlir::SymbolRefAttr getCallee()
void setTemplateParamsAttr(::mlir::ArrayAttr attr)
::mlir::Value getSelfValueFromConstrain()
Return the "self" value (i.e.
::llzk::component::StructType getSingleResultTypeOfWitnessGen()
Assuming the callee contains witness generation code, return the single StructType result.
void setCalleeAttr(::mlir::SymbolRefAttr attr)
::mlir::FailureOr<::llzk::SymbolLookupResult<::llzk::function::FuncDefOp > > getCalleeTarget(::mlir::SymbolTableCollection &tables)
Resolve and return the target FuncDefOp for this CallOp.
::mlir::FunctionType getFunctionType()
static PodType get(::mlir::MLIRContext *context, ::llvm::ArrayRef<::llzk::pod::RecordAttr > records)
::llvm::ArrayRef<::llzk::pod::RecordAttr > getRecords() const
::mlir::FlatSymbolRefAttr getConstNameAttr()
::mlir::Region & getBodyRegion()
OpT getConstNamed(::mlir::StringRef find)
Return the op of type OpT with the given name within the body region if it exists,...
::mlir::StringAttr getSymNameAttr()
void setSymName(::llvm::StringRef attrValue)
::llvm::StringRef getSymName()
inline ::llvm::iterator_range<::mlir::Region::op_iterator< OpT > > getConstOps()
Return ops of type OpT within the body region.
::std::optional<::mlir::Type > getTypeOpt()
::mlir::TypedValue<::mlir::Type > getInput()
::mlir::TypedValue<::mlir::Type > getResult()
::mlir::FailureOr< SymbolLookupResult< function::FuncDefOp > > getFuncTarget(::mlir::SymbolTableCollection &tables)
Return the FuncDefOp that this contract targets, or failure if it does not target a function or the f...
void setTargetAttr(::mlir::SymbolRefAttr attr)
::mlir::FunctionType getFunctionType()
::mlir::Region & getBody()
::mlir::FailureOr< SymbolLookupResult< component::StructDefOp > > getStructTarget(::mlir::SymbolTableCollection &tables)
Return the StructDefOp that this contract targets, or failure if it does not target a struct or the s...
::mlir::SymbolRefAttr getTargetAttr()
::mlir::SymbolRefAttr getCalleeAttr()
void setCalleeAttr(::mlir::SymbolRefAttr attr)
::mlir::FailureOr<::llzk::SymbolLookupResult<::llzk::verif::ContractOp > > getCalleeTarget(::mlir::SymbolTableCollection &tables)
Resolve and return the target Contract for this CallOp.
void setTemplateParamsAttr(::mlir::ArrayAttr attr)
component::StructType getStructTypeWithParams(mlir::SymbolRefAttr nameRef, mlir::ArrayAttr params)
Build a struct type while representing an empty parameter list as absent.
InstantiationLayout buildInstantiationLayout(TemplateOp parentTemplate, mlir::ArrayAttr callParams, const llvm::DenseMap< mlir::Attribute, mlir::Attribute > ¶mNameToConcrete)
Derive the instantiated template name and the remaining explicit parameters that should stay on the r...
array::ArrayType flattenInstantiatedArrayType(array::ArrayType inputTy, mlir::Type convertedElemTy)
Merge nested array dimensions produced by replacing an array element type.
constexpr char FUNC_NAME_COMPUTE[]
Symbol name for the witness generation (and resp.
mlir::SymbolRefAttr getPrefixAsSymbolRefAttr(mlir::SymbolRefAttr symbol)
Return SymbolRefAttr like the one given but with the leaf/final element removed.
mlir::FailureOr< SymbolLookupResultUntyped > lookupTopLevelSymbol(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true)
constexpr char FUNC_NAME_CONSTRAIN[]
mlir::DenseMap< std::pair< mlir::SymbolRefAttr, Side >, mlir::Attribute > UnificationMap
Optional result from type unifications.
bool isNullOrEmpty(mlir::ArrayAttr a)
OpClass getParentOfType(mlir::Operation *op)
Return the closest surrounding parent/ancestor operation that is of type 'OpClass'.
constexpr char FUNC_NAME_PRODUCT[]
constexpr T checkedCast(U u) noexcept
bool isDynamic(IntegerAttr intAttr)
mlir::SymbolRefAttr asSymbolRefAttr(mlir::StringAttr root, mlir::SymbolRefAttr tail)
Build a SymbolRefAttr that prepends tail with root, i.e., root::tail.
bool isTypeVarFreeType(Type type)
bool typeParamsUnify(const ArrayRef< Attribute > &lhsParams, const ArrayRef< Attribute > &rhsParams, UnificationMap *unifications)
bool isConcreteStructParamAttr(mlir::Attribute attr, bool allowStructParams=true)
Return true if attr is a concrete argument for a parameterized struct type.
bool hasParentThatIsa(mlir::Operation *op)
Return true if the parameter has a parent/ancestor op that is an instance of one of the template type...
llvm::SmallVector< FlatSymbolRefAttr > getPieces(SymbolRefAttr ref)
FailureOr< SymbolRefAttr > getPathFromRoot(SymbolOpInterface to, ModuleOp *foundRoot)
Groups the information needed after concrete parameters have been chosen to decide how to name a new ...
std::string templateNameWithAttrs
mlir::ArrayAttr rewrittenCallParams
mlir::SmallVector< mlir::Attribute > remainingNames