Skip to content

Commit 8506d8a

Browse files
authored
[Comgr] Add AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV action (#521)
2 parents 90735af + faa3ea5 commit 8506d8a

File tree

6 files changed

+114
-1
lines changed

6 files changed

+114
-1
lines changed

amd/comgr/include/amd_comgr.h.in

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1823,10 +1823,25 @@ typedef enum amd_comgr_action_kind_s {
18231823
*/
18241824
AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC = 0x13,
18251825

1826+
/**
1827+
* Compile each HIP source data object in @p input in order. For each
1828+
* successful compilation add a SPIR-V data object to @p result. Resolve
1829+
* any include source names using the names of include data objects in
1830+
* @p input. Resolve any include relative path names using the working
1831+
* directory path in @p info. Compile the source for the language in @p
1832+
* info.
1833+
*
1834+
* Return @p AMD_COMGR_STATUS_ERROR if any compilation fails.
1835+
*
1836+
* Return @p AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT if language is not
1837+
* HIP in @p info.
1838+
*/
1839+
AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV = 0x14,
1840+
18261841
/**
18271842
* Marker for last valid action kind.
18281843
*/
1829-
AMD_COMGR_ACTION_LAST = AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC
1844+
AMD_COMGR_ACTION_LAST = AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV
18301845
} amd_comgr_action_kind_t;
18311846

18321847
/**

amd/comgr/src/comgr-compiler.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,6 +2157,42 @@ amd_comgr_status_t AMDGPUCompiler::compileSpirvToRelocatable() {
21572157
return processFiles(AMD_COMGR_DATA_KIND_RELOCATABLE, ".o", TranslatedSpirv);
21582158
}
21592159

2160+
amd_comgr_status_t AMDGPUCompiler::compileSourceToSpirv() {
2161+
if (auto Status = createTmpDirs()) {
2162+
return Status;
2163+
}
2164+
2165+
if (ActionInfo->Language != AMD_COMGR_LANGUAGE_HIP) {
2166+
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
2167+
}
2168+
2169+
if (auto Status = addIncludeFlags()) {
2170+
return Status;
2171+
}
2172+
2173+
if (auto Status = addCompilationFlags()) {
2174+
return Status;
2175+
}
2176+
2177+
// Add SPIRV-specific compilation flags
2178+
Args.push_back("--offload-arch=amdgcnspirv");
2179+
Args.push_back("--no-gpu-bundle-output");
2180+
Args.push_back("-c");
2181+
2182+
2183+
#if _WIN32
2184+
Args.push_back("-fshort-wchar");
2185+
#endif
2186+
2187+
if (ActionInfo->ShouldLinkDeviceLibs) {
2188+
if (auto Status = addDeviceLibraries()) {
2189+
return Status;
2190+
}
2191+
}
2192+
2193+
return processFiles(AMD_COMGR_DATA_KIND_SPIRV, ".spv");
2194+
}
2195+
21602196
AMDGPUCompiler::AMDGPUCompiler(DataAction *ActionInfo, DataSet *InSet,
21612197
DataSet *OutSet, raw_ostream &LogS)
21622198
: ActionInfo(ActionInfo), InSet(InSet), OutSetT(DataSet::convert(OutSet)),

amd/comgr/src/comgr-compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class AMDGPUCompiler {
8181
amd_comgr_status_t compileToExecutable();
8282
amd_comgr_status_t compileSpirvToRelocatable();
8383
amd_comgr_status_t translateSpirvToBitcode();
84+
amd_comgr_status_t compileSourceToSpirv();
8485

8586
amd_comgr_language_t getLanguage() const { return ActionInfo->Language; }
8687
};

amd/comgr/src/comgr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ amd_comgr_status_t dispatchCompilerAction(amd_comgr_action_kind_t ActionKind,
102102
return Compiler.compileSpirvToRelocatable();
103103
case AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC:
104104
return Compiler.translateSpirvToBitcode();
105+
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV:
106+
return Compiler.compileSourceToSpirv();
105107

106108
default:
107109
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
@@ -193,6 +195,8 @@ StringRef getActionKindName(amd_comgr_action_kind_t ActionKind) {
193195
return "AMD_COMGR_ACTION_COMPILE_SPIRV_TO_RELOCATABLE";
194196
case AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC:
195197
return "AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC";
198+
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV:
199+
return "AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV";
196200
}
197201

198202
llvm_unreachable("invalid action");
@@ -1302,6 +1306,7 @@ amd_comgr_status_t AMD_COMGR_API
13021306
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE:
13031307
case AMD_COMGR_ACTION_COMPILE_SPIRV_TO_RELOCATABLE:
13041308
case AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC:
1309+
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV:
13051310
ActionStatus = dispatchCompilerAction(ActionKind, ActionInfoP, InputSetP,
13061311
ResultSetP, *LogP);
13071312
break;

amd/comgr/test-lit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_comgr_lit_binary(source-to-bc-with-dev-libs c)
4242
add_comgr_lit_binary(spirv-translator c)
4343
add_comgr_lit_binary(compile-opencl-minimal c)
4444
add_comgr_lit_binary(spirv-to-reloc c)
45+
add_comgr_lit_binary(source-to-spirv c)
4546
add_comgr_lit_binary(unbundle c)
4647
add_comgr_lit_binary(get-version c)
4748
add_comgr_lit_binary(status-string c)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===- source-to-spirv.c --------------------------------------------------===//
2+
//
3+
// Part of Comgr, under the Apache License v2.0 with LLVM Exceptions. See
4+
// amd/comgr/LICENSE.TXT in this repository for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "amd_comgr.h"
10+
#include "common.h"
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
15+
int main(int argc, char *argv[]) {
16+
char *BufSource;
17+
size_t SizeSource;
18+
amd_comgr_data_t DataSource;
19+
amd_comgr_data_set_t DataSetSource, DataSetSpirv;
20+
amd_comgr_action_info_t DataAction;
21+
size_t Count;
22+
23+
if (argc != 3) {
24+
fprintf(stderr, "Usage: source-to-spirv file.hip file.spv\n");
25+
exit(1);
26+
}
27+
28+
SizeSource = setBuf(argv[1], &BufSource);
29+
30+
amd_comgr_(create_data(AMD_COMGR_DATA_KIND_SOURCE, &DataSource));
31+
amd_comgr_(set_data(DataSource, SizeSource, BufSource));
32+
amd_comgr_(set_data_name(DataSource, "file.hip"));
33+
34+
amd_comgr_(create_data_set(&DataSetSource));
35+
amd_comgr_(data_set_add(DataSetSource, DataSource));
36+
37+
amd_comgr_(create_action_info(&DataAction));
38+
amd_comgr_(action_info_set_language(DataAction, AMD_COMGR_LANGUAGE_HIP));
39+
40+
amd_comgr_(create_data_set(&DataSetSpirv));
41+
amd_comgr_(do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV,
42+
DataAction, DataSetSource, DataSetSpirv));
43+
44+
amd_comgr_data_t DataSpirv;
45+
amd_comgr_(action_data_get_data(DataSetSpirv, AMD_COMGR_DATA_KIND_SPIRV,
46+
0, &DataSpirv));
47+
dumpData(DataSpirv, argv[2]);
48+
49+
amd_comgr_(release_data(DataSource));
50+
amd_comgr_(destroy_data_set(DataSetSource));
51+
amd_comgr_(destroy_data_set(DataSetSpirv));
52+
amd_comgr_(destroy_action_info(DataAction));
53+
free(BufSource);
54+
}
55+

0 commit comments

Comments
 (0)