[K/N] Add option to save unoptimized bitcode

Add `-Xsave-llvm-ir` option that stores LLVM IR text into temporary
directory right after IrToBitcode translation. This is required for
FileCheck-based tests and just helpful during development.
This commit is contained in:
Sergey Bogolepov
2021-09-29 13:48:22 +07:00
committed by Space
parent a1424489b7
commit 7a3f33ad0b
4 changed files with 21 additions and 2 deletions
@@ -175,6 +175,7 @@ class K2Native : CLICompiler<K2NativeCompilerArguments>() {
arguments.manifestFile ?.let{ put(MANIFEST_FILE, it) }
arguments.runtimeFile ?.let{ put(RUNTIME_FILE, it) }
arguments.temporaryFilesDir?.let { put(TEMPORARY_FILES_DIR, it) }
put(SAVE_LLVM_IR, arguments.saveLlvmIr)
put(LIST_TARGETS, arguments.listTargets)
put(OPTIMIZATION, arguments.optimization)
@@ -255,6 +255,9 @@ class K2NativeCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xtemporary-files-dir", deprecatedName = "--temporary_files_dir", valueDescription = "<path>", description = "Save temporary files to the given directory")
var temporaryFilesDir: String? = null
@Argument(value = "-Xsave-llvm-ir", description = "Save result of Kotlin IR to LLVM IR translation to the temporary files directory.")
var saveLlvmIr: Boolean = false
@Argument(value = "-Xverify-bitcode", deprecatedName = "--verify_bitcode", description = "Verify llvm bitcode after each method")
var verifyBitCode: Boolean = false
@@ -134,6 +134,8 @@ class KonanConfigKeys {
= CompilerConfigurationKey.create("target we compile for")
val TEMPORARY_FILES_DIR: CompilerConfigurationKey<String?>
= CompilerConfigurationKey.create("directory for temporary files")
val SAVE_LLVM_IR: CompilerConfigurationKey<Boolean>
= CompilerConfigurationKey.create("save LLVM IR")
val VERIFY_BITCODE: CompilerConfigurationKey<Boolean>
= CompilerConfigurationKey.create("verify bitcode")
val VERIFY_IR: CompilerConfigurationKey<Boolean>
@@ -5,6 +5,10 @@
package org.jetbrains.kotlin.backend.konan.llvm
import kotlinx.cinterop.alloc
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.toKStringFromUtf8
import llvm.*
import org.jetbrains.kotlin.backend.common.phaser.CompilerPhase
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
@@ -12,7 +16,6 @@ import org.jetbrains.kotlin.backend.common.phaser.PhaserState
import org.jetbrains.kotlin.backend.common.phaser.namedUnitPhase
import org.jetbrains.kotlin.backend.konan.*
import org.jetbrains.kotlin.backend.konan.descriptors.GlobalHierarchyAnalysis
import org.jetbrains.kotlin.backend.konan.lower.DECLARATION_ORIGIN_BRIDGE_METHOD
import org.jetbrains.kotlin.backend.konan.lower.InlineClassPropertyAccessorsLowering
import org.jetbrains.kotlin.backend.konan.lower.RedundantCoercionsCleaner
import org.jetbrains.kotlin.backend.konan.lower.ReturnsInsertionLowering
@@ -27,7 +30,6 @@ import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.*
import org.jetbrains.kotlin.util.OperatorNameConventions
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
internal val contextLLVMSetupPhase = makeKonanModuleOpPhase(
name = "ContextLLVMSetup",
@@ -325,6 +327,17 @@ internal val codegenPhase = makeKonanModuleOpPhase(
description = "Code generation",
op = { context, irModule ->
irModule.acceptVoid(context.codegenVisitor)
// TODO: Consider adding LLVM_IR compiler output kind.
if (context.configuration.getBoolean(KonanConfigKeys.SAVE_LLVM_IR)) {
val moduleName: String = memScoped {
val sizeVar = alloc<size_tVar>()
LLVMGetModuleIdentifier(context.llvmModule, sizeVar.ptr)!!.toKStringFromUtf8()
}
val output = context.config.tempFiles.create(moduleName,".ll")
if (LLVMPrintModuleToFile(context.llvmModule, output.absolutePath, null) != 0) {
error("Can't dump LLVM IR to ${output.absolutePath}")
}
}
}
)