Skip to content

Commit

Permalink
[AlignModuleStructsPass] Update types in constants
Browse files Browse the repository at this point in the history
Execution.Regression_35_Constant_Struct_Alignment reveals on LLVM 20
that if a constant expression references a type that needs to be
replaced, we were ignoring it unless the constant expression also
happened to contain references to globals that need to be replaced. Do
a pass over constant operands and update them to instructions as needed.
  • Loading branch information
hvdijk committed Oct 24, 2024
1 parent 6c02bae commit 745fc40
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<GEPOperator>(C)) {
if (getNewType(GEP->getSourceElementType(), typeMap)) {
compiler::utils::replaceConstantExpressionWithInstruction(C);
return true;
}
}
for (auto *Op : C->operand_values()) {
if (replaceConstantsForRemapping(cast<Constant>(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.
///
Expand All @@ -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<Constant>(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<Function *, 4> funcs;
Expand Down

0 comments on commit 745fc40

Please sign in to comment.