Skip to content

Commit dcb00cd

Browse files
LLVM: Emit most declared definitions as private (#946)
I was experimenting with making emitted function definitions `private` by default in llvm. This seemed to only reduce the code size of our *.opt.ll files, which might not even be wanted, because now all the unused/inlined function definitions are gone. Benchmarking this showed no improvement *except* for "examples/benchmarks/are_we_fast_yet/queens.effekt" which unexpectedly got 30% faster. Not sure what is going on
1 parent f4823f3 commit dcb00cd

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

effekt/shared/src/main/scala/effekt/generator/llvm/PrettyPrinter.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ object PrettyPrinter {
1212
definitions.map(show).mkString("\n\n")
1313

1414
def show(definition: Definition)(using C: Context): LLVMString = definition match {
15-
case Function(callingConvention, returnType, name, parameters, basicBlocks) =>
15+
case Function(linkage, callingConvention, returnType, name, parameters, basicBlocks) =>
1616
s"""
17-
define ${show(callingConvention)} ${show(returnType)} ${globalName(name)}(${commaSeparated(parameters.map(show))}) {
17+
define ${show(linkage)} ${show(callingConvention)} ${show(returnType)} ${globalName(name)}(${commaSeparated(parameters.map(show))}) {
1818
${indentedLines(basicBlocks.map(show).mkString)}
1919
}
2020
"""
@@ -38,6 +38,11 @@ define ${show(callingConvention)} ${show(returnType)} ${globalName(name)}(${comm
3838
s"@$name = private constant ${show(initializer)}"
3939
}
4040

41+
def show(linkage: Linkage): LLVMString = linkage match {
42+
case External() => "external"
43+
case Private() => "private"
44+
}
45+
4146
def show(callingConvention: CallingConvention): LLVMString = callingConvention match {
4247
case Ccc() => "ccc"
4348
case Tailcc(_) => "tailcc"

effekt/shared/src/main/scala/effekt/generator/llvm/Transformer.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Transformer {
2626
Call("stack", Ccc(), stackType, withEmptyStack, List()),
2727
Call("_", Tailcc(false), VoidType(), transform(entry), List(LocalReference(stackType, "stack"))))
2828
val entryBlock = BasicBlock("entry", entryInstructions, RetVoid())
29-
val entryFunction = Function(Ccc(), VoidType(), "effektMain", List(), List(entryBlock))
29+
val entryFunction = Function(External(), Ccc(), VoidType(), "effektMain", List(), List(entryBlock))
3030

3131
declarations.map(transform) ++ globals :+ entryFunction
3232
}
@@ -434,7 +434,7 @@ object Transformer {
434434
val instructions = BC.instructions; BC.instructions = null;
435435

436436
val entryBlock = BasicBlock("entry", instructions, terminator);
437-
val function = Function(Ccc(), VoidType(), name, parameters, entryBlock :: basicBlocks);
437+
val function = Function(Private(), Ccc(), VoidType(), name, parameters, entryBlock :: basicBlocks);
438438

439439
emit(function)
440440
}
@@ -449,7 +449,7 @@ object Transformer {
449449
val instructions = BC.instructions; BC.instructions = null;
450450

451451
val entryBlock = BasicBlock("entry", instructions, terminator);
452-
val function = Function(Tailcc(true), VoidType(), name, parameters :+ Parameter(stackType, "stack"), entryBlock :: basicBlocks);
452+
val function = Function(Private(), Tailcc(true), VoidType(), name, parameters :+ Parameter(stackType, "stack"), entryBlock :: basicBlocks);
453453

454454
emit(function)
455455
}

effekt/shared/src/main/scala/effekt/generator/llvm/Tree.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ package llvm
77
* see: https://hackage.haskell.org/package/llvm-hs-pure-9.0.0/docs/LLVM-AST.html#t:Definition
88
*/
99
enum Definition {
10-
case Function(callingConvention: CallingConvention, returnType: Type, name: String, parameters: List[Parameter], basicBlocks: List[BasicBlock])
10+
case Function(linkage: Linkage, callingConvention: CallingConvention, returnType: Type, name: String, parameters: List[Parameter], basicBlocks: List[BasicBlock])
1111
case VerbatimFunction(callingConvention: CallingConvention, returnType: Type, name: String, parameters: List[Parameter], body: String)
1212
case Verbatim(content: String)
1313
case GlobalConstant(name: String, initializer: Operand) // initializer should be constant
1414
}
1515
export Definition.*
1616

17+
enum Linkage {
18+
case External()
19+
case Private()
20+
}
21+
export Linkage.*
22+
1723
enum CallingConvention {
1824
case Ccc()
1925
case Tailcc(musttail: Boolean)

0 commit comments

Comments
 (0)