Skip to content

Commit 7e5bd4b

Browse files
committed
[Comgr] Add compile-to-exec action and LLVM-IR language
In this patch, we add a new action to allow compilation from source directly into an executable (.so) file. We also add support for a new language type, LLVM-IR, which allows users to pass .ll and .bc files to AMD_COMGR_COMPILE_* actions as input. Change-Id: I111a10a9b09d1cda84a24579c7ea144f52a6041c
1 parent 29a03a7 commit 7e5bd4b

File tree

7 files changed

+325
-2
lines changed

7 files changed

+325
-2
lines changed

amd/comgr/docs/ReleaseNotes.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ of the flag handling to toolchain. Currently only supports HIP.
9898
- (Data Type) AMD\_COMGR\_DATA\_KIND\_AR\_BUNDLE
9999
- These data kinds can now be passed to an AMD\_COMGR\_ACTION\_LINK\_BC\_TO\_BC
100100
action, and Comgr will internally unbundle and link via the OffloadBundler and linkInModule APIs.
101+
- (Language Type) AMD\_COMGR\_LANGUAGE\_LLVM\_IR
102+
- This language can now be passed to AMD\_COMGR\_ACTION\_COMPILE\_\* actions
103+
to enable compilation of LLVM IR (.ll or .bc) files. This is useful for MLIR
104+
contexts.
105+
- (Action) AMD\_COMGR\_ACTION\_COMPILE\_SOURCE\_TO\_EXECUTABLE
106+
- This action allows compilation from source directly to executable, including
107+
linking device libraries.
108+
101109

102110
Deprecated Comgr Actions and Data Types
103111
---------------------------------------

amd/comgr/include/amd_comgr.h.in

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,14 @@ typedef enum amd_comgr_language_s {
273273
* HIP.
274274
*/
275275
AMD_COMGR_LANGUAGE_HIP = 0x4,
276+
/**
277+
* LLVM IR, either textual (.ll) or bitcode (.bc) format.
278+
*/
279+
AMD_COMGR_LANGUAGE_LLVM_IR = 0x5,
276280
/**
277281
* Marker for last valid language.
278282
*/
279-
AMD_COMGR_LANGUAGE_LAST = AMD_COMGR_LANGUAGE_HIP
283+
AMD_COMGR_LANGUAGE_LAST = AMD_COMGR_LANGUAGE_LLVM_IR
280284
} amd_comgr_language_t;
281285

282286
/**
@@ -1768,10 +1772,26 @@ typedef enum amd_comgr_action_kind_s {
17681772
* if isa name or language is not set in @p info.
17691773
*/
17701774
AMD_COMGR_ACTION_COMPILE_SOURCE_TO_RELOCATABLE = 0x10,
1775+
/**
1776+
* Compile each source data object in @p input and create a single executabele
1777+
* in @p result. Resolve any include source names using the names of include
1778+
* data objects in @p input. Resolve any include relative path names using the
1779+
* working directory path in @p info. Produce executable for isa name in @p
1780+
* info. Compile the source for the language in @p info. Link against
1781+
* the device-specific and language-specific bitcode device libraries
1782+
* required for compilation.
1783+
*
1784+
* Return @p AMD_COMGR_STATUS_ERROR if any compilation
1785+
* fails.
1786+
*
1787+
* Return @p AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT
1788+
* if isa name or language is not set in @p info.
1789+
*/
1790+
AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE = 0x11,
17711791
/**
17721792
* Marker for last valid action kind.
17731793
*/
1774-
AMD_COMGR_ACTION_LAST = AMD_COMGR_ACTION_COMPILE_SOURCE_TO_RELOCATABLE
1794+
AMD_COMGR_ACTION_LAST = AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE
17751795
} amd_comgr_action_kind_t;
17761796

17771797
/**

amd/comgr/src/comgr-compiler.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,9 @@ amd_comgr_status_t AMDGPUCompiler::addCompilationFlags() {
10391039
Args.push_back("-x");
10401040

10411041
switch (ActionInfo->Language) {
1042+
case AMD_COMGR_LANGUAGE_LLVM_IR:
1043+
Args.push_back("ir");
1044+
break;
10421045
case AMD_COMGR_LANGUAGE_OPENCL_1_2:
10431046
Args.push_back("cl");
10441047
Args.push_back("-std=cl1.2");
@@ -1151,6 +1154,36 @@ amd_comgr_status_t AMDGPUCompiler::compileToBitcode(bool WithDeviceLibs) {
11511154
return processFiles(AMD_COMGR_DATA_KIND_BC, ".bc");
11521155
}
11531156

1157+
amd_comgr_status_t AMDGPUCompiler::compileToExecutable() {
1158+
if (auto Status = createTmpDirs()) {
1159+
return Status;
1160+
}
1161+
1162+
if (ActionInfo->IsaName) {
1163+
if (auto Status = addTargetIdentifierFlags(ActionInfo->IsaName, true)) {
1164+
return Status;
1165+
}
1166+
}
1167+
1168+
if (auto Status = addIncludeFlags()) {
1169+
return Status;
1170+
}
1171+
1172+
if (auto Status = addCompilationFlags()) {
1173+
return Status;
1174+
}
1175+
1176+
#if _WIN32
1177+
Args.push_back("-fshort-wchar");
1178+
#endif
1179+
1180+
if (auto Status = addDeviceLibraries()) {
1181+
return Status;
1182+
}
1183+
1184+
return processFiles(AMD_COMGR_DATA_KIND_EXECUTABLE, ".so");
1185+
}
1186+
11541187
amd_comgr_status_t AMDGPUCompiler::compileToRelocatable() {
11551188
if (auto Status = createTmpDirs()) {
11561189
return Status;

amd/comgr/src/comgr-compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class AMDGPUCompiler {
150150
amd_comgr_status_t linkToRelocatable();
151151
amd_comgr_status_t linkToExecutable();
152152
amd_comgr_status_t compileToFatBin();
153+
amd_comgr_status_t compileToExecutable();
153154

154155
amd_comgr_language_t getLanguage() const { return ActionInfo->Language; }
155156
};

amd/comgr/src/comgr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ dispatchCompilerAction(amd_comgr_action_kind_t ActionKind,
182182
return Compiler.compileToRelocatable();
183183
case AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC:
184184
return Compiler.compileToBitcode(true);
185+
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE:
186+
return Compiler.compileToExecutable();
185187

186188
default:
187189
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
@@ -242,6 +244,8 @@ StringRef getActionKindName(amd_comgr_action_kind_t ActionKind) {
242244
return "AMD_COMGR_ACTION_COMPILE_SOURCE_TO_FATBIN";
243245
case AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC:
244246
return "AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC";
247+
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE:
248+
return "AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE";
245249
}
246250

247251
llvm_unreachable("invalid action");
@@ -259,6 +263,8 @@ static StringRef getLanguageName(amd_comgr_language_t Language) {
259263
return "AMD_COMGR_LANGUAGE_HC";
260264
case AMD_COMGR_LANGUAGE_HIP:
261265
return "AMD_COMGR_LANGUAGE_HIP";
266+
case AMD_COMGR_LANGUAGE_LLVM_IR:
267+
return "AMD_COMGR_LANGUAGE_LLVM_IR";
262268
}
263269

264270
llvm_unreachable("invalid language");
@@ -1360,6 +1366,7 @@ amd_comgr_status_t AMD_COMGR_API
13601366
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_RELOCATABLE:
13611367
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_FATBIN:
13621368
case AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC:
1369+
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE:
13631370
ActionStatus = dispatchCompilerAction(ActionKind, ActionInfoP, InputSetP,
13641371
ResultSetP, *LogP);
13651372
break;

amd/comgr/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ add_test_input_binary(reloc2 source/reloc2.cl source/reloc2.o -c -mcode-object-v
9191
add_test_input_binary(reloc-asm source/reloc-asm.s source/reloc-asm.o -c -mcode-object-version=4)
9292
add_test_input_binary(shared source/shared.cl source/shared.so -mcode-object-version=4)
9393
add_test_input_binary(shared-debug source/shared.cl source/shared-debug.so -g -mcode-object-version=4)
94+
add_test_input_bitcode(source1 source/source1.cl source/source1.bc)
9495

9596
configure_file("source/source1.cl" "source/source1.cl" COPYONLY)
9697
configure_file("source/source2.cl" "source/source2.cl" COPYONLY)
@@ -201,6 +202,7 @@ add_comgr_test(multithread_test cpp)
201202
add_comgr_test(name_expression_map_test c)
202203
add_comgr_test(nested_kernel_test c)
203204
add_comgr_test(map_elf_virtual_address_test c)
205+
add_comgr_test(compile_source_to_executable c)
204206

205207
# Test : Compile HIP tests only if HIP-Clang is installed.
206208
if (DEFINED HIP_COMPILER AND "${HIP_COMPILER}" STREQUAL "clang")

0 commit comments

Comments
 (0)