diff --git a/modules/compiler/compiler_pipeline/source/align_module_structs_pass.cpp b/modules/compiler/compiler_pipeline/source/align_module_structs_pass.cpp index 8355b0633..c79e2cc58 100644 --- a/modules/compiler/compiler_pipeline/source/align_module_structs_pass.cpp +++ b/modules/compiler/compiler_pipeline/source/align_module_structs_pass.cpp @@ -356,6 +356,30 @@ Type *getNewType(Value *v, const StructReplacementMap &typeMap) { return getNewType(v->getType(), typeMap); } +bool replaceConstantsForRemapping(Constant *C, + const StructReplacementMap &typeMap, + const ValueToValueMapTy &valMap) { + if (valMap.find(C) != valMap.end()) { + return false; + } + if (getNewType(C->getType(), typeMap)) { + compiler::utils::replaceConstantExpressionWithInstruction(C); + return true; + } + if (auto *GEP = dyn_cast(C)) { + if (getNewType(GEP->getSourceElementType(), typeMap)) { + compiler::utils::replaceConstantExpressionWithInstruction(C); + return true; + } + } + for (auto *Op : C->operand_values()) { + if (replaceConstantsForRemapping(cast(Op), typeMap, valMap)) { + return true; + } + } + return false; +} + /// @brief Performs the update on the LLVM module to replace all the Values /// using the old struct type with Values using our padded variant. /// @@ -378,6 +402,19 @@ void replaceModuleTypes(const StructReplacementMap &typeMap, Module &module) { replaceGlobalVariable(global, typeMap, valMap); } + // Replace constants if they are using types that need to be remapped. + for (auto &func : module.functions()) { + for (BasicBlock &block : func) { + for (Instruction &inst : block) { + for (auto *op : inst.operand_values()) { + if (auto *c = dyn_cast(op)) { + replaceConstantsForRemapping(c, typeMap, valMap); + } + } + } + } + } + // Identify all functions which use a struct type and need to be cloned, // this is to avoid unnecessary work in the clone. SmallVector funcs;