Skip to content

Commit

Permalink
Fixed reference elimination when params are dereffd
Browse files Browse the repository at this point in the history
  • Loading branch information
leventeBajczi committed Nov 14, 2024
1 parent 82efcc2 commit 62dd365
Showing 1 changed file with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ class ReferenceElimination(val parseContext: ParseContext) : ProcedurePass {
Pair(varDecl, SequenceLabel(labels))
}

if (builder.parent.getInitProcedures().any { it.first == builder }) {
addRefInitializations(builder, globalReferredVars) // we only need this for main
}
addRefInitializations(builder, referredVars)

val allReferredVars = referredVars + globalReferredVars
if (allReferredVars.isNotEmpty()) {
val edges = LinkedHashSet(builder.getEdges())
Expand All @@ -141,6 +136,11 @@ class ReferenceElimination(val parseContext: ParseContext) : ProcedurePass {
edge.withLabel(edge.label.changeReferredVars(allReferredVars, parseContext))
)
}
if (builder.parent.getInitProcedures().any { it.first == builder }) {
addRefInitializations(builder, globalReferredVars) // we only need this for main
}
addRefInitializations(builder, referredVars)

return DeterministicPass().run(NormalizePass().run(builder))
}

Expand All @@ -163,7 +163,38 @@ class ReferenceElimination(val parseContext: ParseContext) : ProcedurePass {
it.stmt is AssignStmt<*> &&
it.stmt.varDecl.let { it.name == "__theta_ptr_size" && it.type is ArrayType<*, *> }
}
val newLabelOrder = listOfNotNull(sizeInit) + initLabels + labels.filter { it != sizeInit }
val spInit =
labels.find {
it is StmtLabel &&
it.stmt is AssignStmt<*> &&
it.stmt.varDecl == builder.parent.ptrVar(parseContext)
}
val touchedParams =
builder.getParams().filter {
it.second != ParamDirection.OUT && referredVars.containsKey(it.first)
}
val paramMapping =
if (touchedParams.isNotEmpty()) {
touchedParams.map {
val type = referredVars[it.first]!!.first.type
StmtLabel(
MemoryAssignStmt.create(
Dereference(
cast(referredVars[it.first]!!.first.ref, type),
cast(CComplexType.getSignedLong(parseContext).nullValue, type),
it.first.type,
),
it.first.ref,
)
)
}
} else listOf()
val newLabelOrder =
listOfNotNull(spInit) +
listOfNotNull(sizeInit) +
initLabels +
paramMapping +
labels.filter { it != sizeInit && it != spInit }
it.withLabel(SequenceLabel(newLabelOrder, it.label.metadata))
}
initEdges.forEach(builder::removeEdge)
Expand Down

0 comments on commit 62dd365

Please sign in to comment.