Skip to content

Commit

Permalink
optimization: use puts for simple strings & inline getelementptr calls
Browse files Browse the repository at this point in the history
  • Loading branch information
SrGaabriel committed Sep 9, 2024
1 parent 692f432 commit 0d412e2
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions backend/llvm/src/commonMain/kotlin/intrinsic/Print.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PrintlnIntrinsicFunctionExecutor : IntrinsicFunctionExecutor<DragonHookCon
if (formatName == null || formatCode == null) {
context.functionDsl.run {
callExternal(
functionName = "printf",
functionName = "puts",
returnType = DragonType.Int32,
arguments = listOf(context.argumentValues.single()),
definition = listOf(DragonType.Pointer(DragonType.Int8)),
Expand All @@ -33,7 +33,7 @@ class PrintlnIntrinsicFunctionExecutor : IntrinsicFunctionExecutor<DragonHookCon
}

context.functionDsl.run {
val format = useFormat(formatName, formatCode).assign(constantOverride = true)
val format = useFormat(formatName, formatCode)
callExternal(
functionName = "printf",
returnType = DragonType.Int32,
Expand Down
12 changes: 6 additions & 6 deletions bard/output/ll/bard.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
\00"
declare i32 @printf(i8*, ...)
@str_1613095350 = unnamed_addr constant [14 x i8] c"Hello, World!\00"
declare i32 @puts(i8*)

define i32 @test() {
ret i32 8
}
define i32 @main() {
%1 = call i32 @test()
%2 = getelementptr [4 x i8], [4 x i8]* @formatln_int32, i32 0, i32 0
call i32 @printf(i8* %2, i32 %1)
call i32 @printf(i8* %2, i32 %1)
call i32 @printf(i8* %2, i32 %1)
%6 = getelementptr [14 x i8], [14 x i8]* @str_1613095350, i32 0, i32 0
call i32 @printf(i8* %6)
call i32 @printf(i8* getelementptr ([4 x i8], [4 x i8]* @formatln_int32, i32 0, i32 0), i32 %1)
call i32 @printf(i8* getelementptr ([4 x i8], [4 x i8]* @formatln_int32, i32 0, i32 0), i32 %1)
call i32 @printf(i8* getelementptr ([4 x i8], [4 x i8]* @formatln_int32, i32 0, i32 0), i32 %1)
%5 = getelementptr [14 x i8], [14 x i8]* @str_1613095350, i32 0, i32 0
call i32 @puts(i8* %5)
ret i32 %1
}
Binary file modified bard/output/output.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion ryujin/src/commonMain/kotlin/statement/ControlFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ReturnStatement(
) : DragonStatement {
override val memoryDependencies: Set<Value> = setOf(value)

override fun llvm(): String {
override fun statementLlvm(): String {
if (value.type == DragonType.Void) {
return "ret void"
}
Expand Down
2 changes: 1 addition & 1 deletion ryujin/src/commonMain/kotlin/statement/DragonStatement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sealed interface DragonStatement {

fun isValid(): Boolean = true

fun llvm(): String
fun statementLlvm(): String
}

interface TypedDragonStatement : DragonStatement {
Expand Down
2 changes: 1 addition & 1 deletion ryujin/src/commonMain/kotlin/statement/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class CallStatement(
) : TypedDragonStatement {
override val memoryDependencies: Set<Value> = arguments.toSet()

override fun llvm(): String {
override fun statementLlvm(): String {
return "call ${type.llvm} @${functionName}(${arguments.joinToString { "${it.type.llvm} ${it.llvm()}" }})"
}
}
2 changes: 1 addition & 1 deletion ryujin/src/commonMain/kotlin/statement/Math.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ class AddStatement(

override val type: DragonType get() = left.type

override fun llvm(): String =
override fun statementLlvm(): String =
"add ${type.llvm} ${left.llvm()}, ${right.llvm()}"
}
6 changes: 3 additions & 3 deletions ryujin/src/commonMain/kotlin/statement/Memory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AllocateStatement(

override val type: DragonType = DragonType.Pointer(allocationType)

override fun llvm(): String {
override fun statementLlvm(): String {
return "alloca ${type.llvm}, align $alignment"
}
}
Expand All @@ -28,7 +28,7 @@ class StoreStatement(
return target.type is DragonType.Pointer
}

override fun llvm(): String {
override fun statementLlvm(): String {
return "store ${value.type.llvm} ${value.llvm()}, ${target.type.llvm} ${target.llvm()}"
}
}
Expand All @@ -46,7 +46,7 @@ class CompositeStoreStatement(
return uniqueType != null || (target.type as? DragonType.Pointer)?.type == uniqueType
}

override fun llvm(): String {
override fun statementLlvm(): String {
return "store ${uniqueType!!.llvm} ${values.joinToString(
prefix = "[",
separator = ", ",
Expand Down
23 changes: 18 additions & 5 deletions ryujin/src/commonMain/kotlin/statement/Pointers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ data class AssignStatement(
): DragonStatement {
override val memoryDependencies: Set<Value> = setOf(memory) + value.memoryDependencies

override fun llvm(): String =
"%${memory.register} = ${value.llvm()}"
override fun statementLlvm(): String =
"%${memory.register} = ${value.statementLlvm()}"
}

data class GetElementPointerStatement(
Expand All @@ -21,7 +21,7 @@ data class GetElementPointerStatement(
val index: Value,
val total: Boolean = true,
val inbounds: Boolean = true
): TypedDragonStatement {
): TypedDragonStatement, Value {
override val memoryDependencies: Set<Value> = setOf(index, struct)

val originalType = if (struct.type !is DragonType.Pointer) {
Expand All @@ -32,7 +32,7 @@ data class GetElementPointerStatement(
val pointerType = DragonType.Pointer(originalType)
override val type: DragonType = DragonType.Pointer(elementType)

override fun llvm(): String =
override fun statementLlvm(): String =
"getelementptr " +
(if (inbounds) {
"inbounds "
Expand All @@ -44,6 +44,19 @@ data class GetElementPointerStatement(
} else {
""
}) + ", i32 ${index.llvm()}"

override fun llvm(): String =
"getelementptr " +
(if (inbounds) {
"inbounds "
} else {
""
}) +
"(${originalType.llvm}, ${pointerType.llvm} ${struct.llvm()}" + (if (total) {
", i32 0"
} else {
""
}) + ", i32 ${index.llvm()})"
}

class LoadStatement(
Expand All @@ -56,7 +69,7 @@ class LoadStatement(

override val type: DragonType = target.type.descendOneLevel()

override fun llvm(): String {
override fun statementLlvm(): String {
require(isValid()) { "Cannot load from non-pointer type" }
return "load ${type.llvm}, ${target.type.llvm} ${target.llvm()}"
}
Expand Down
2 changes: 1 addition & 1 deletion ryujin/src/commonMain/kotlin/struct/Value.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package me.gabriel.ryujin.struct

sealed interface Value {
interface Value {
val type: DragonType

fun llvm(): String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DefaultDragonIrTranscriber: DragonIrTranscriber {
append("define ${function.returnType.llvm} @${function.name}(")
append(function.parameters.joinToString(", ") { "${it.type.llvm} %${it.register}" })
append(") {\n")
function.statements.forEach { append(" ${it.llvm()}\n") }
function.statements.forEach { append(" ${it.statementLlvm()}\n") }
append("}")
}
}
Expand Down

0 comments on commit 0d412e2

Please sign in to comment.