Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion amd/comgr/include/amd_comgr.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1823,10 +1823,25 @@ typedef enum amd_comgr_action_kind_s {
*/
AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC = 0x13,

/**
* Compile each HIP source data object in @p input in order. For each
* successful compilation add a SPIR-V data object to @p result. Resolve
* any include source names using the names of include data objects in
* @p input. Resolve any include relative path names using the working
* directory path in @p info. Compile the source for the language in @p
* info.
*
* Return @p AMD_COMGR_STATUS_ERROR if any compilation fails.
*
* Return @p AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT if language is not
* HIP in @p info.
*/
AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV = 0x14,

/**
* Marker for last valid action kind.
*/
AMD_COMGR_ACTION_LAST = AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC
AMD_COMGR_ACTION_LAST = AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV
} amd_comgr_action_kind_t;

/**
Expand Down
36 changes: 36 additions & 0 deletions amd/comgr/src/comgr-compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2156,6 +2156,42 @@ amd_comgr_status_t AMDGPUCompiler::compileSpirvToRelocatable() {
return processFiles(AMD_COMGR_DATA_KIND_RELOCATABLE, ".o", TranslatedSpirv);
}

amd_comgr_status_t AMDGPUCompiler::compileSourceToSpirv() {
if (auto Status = createTmpDirs()) {
return Status;
}

if (ActionInfo->Language != AMD_COMGR_LANGUAGE_HIP) {
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
}

if (auto Status = addIncludeFlags()) {
return Status;
}

if (auto Status = addCompilationFlags()) {
return Status;
}

// Add SPIRV-specific compilation flags
Args.push_back("--offload-arch=amdgcnspirv");
Args.push_back("--no-gpu-bundle-output");
Args.push_back("-c");


#if _WIN32
Args.push_back("-fshort-wchar");
#endif

if (ActionInfo->ShouldLinkDeviceLibs) {
if (auto Status = addDeviceLibraries()) {
return Status;
}
}

return processFiles(AMD_COMGR_DATA_KIND_SPIRV, ".spv");
}

AMDGPUCompiler::AMDGPUCompiler(DataAction *ActionInfo, DataSet *InSet,
DataSet *OutSet, raw_ostream &LogS)
: ActionInfo(ActionInfo), InSet(InSet), OutSetT(DataSet::convert(OutSet)),
Expand Down
1 change: 1 addition & 0 deletions amd/comgr/src/comgr-compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AMDGPUCompiler {
amd_comgr_status_t compileToExecutable();
amd_comgr_status_t compileSpirvToRelocatable();
amd_comgr_status_t translateSpirvToBitcode();
amd_comgr_status_t compileSourceToSpirv();

amd_comgr_language_t getLanguage() const { return ActionInfo->Language; }
};
Expand Down
5 changes: 5 additions & 0 deletions amd/comgr/src/comgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ amd_comgr_status_t dispatchCompilerAction(amd_comgr_action_kind_t ActionKind,
return Compiler.compileSpirvToRelocatable();
case AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC:
return Compiler.translateSpirvToBitcode();
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV:
return Compiler.compileSourceToSpirv();

default:
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;
Expand Down Expand Up @@ -193,6 +195,8 @@ StringRef getActionKindName(amd_comgr_action_kind_t ActionKind) {
return "AMD_COMGR_ACTION_COMPILE_SPIRV_TO_RELOCATABLE";
case AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC:
return "AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC";
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV:
return "AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV";
}

llvm_unreachable("invalid action");
Expand Down Expand Up @@ -1302,6 +1306,7 @@ amd_comgr_status_t AMD_COMGR_API
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE:
case AMD_COMGR_ACTION_COMPILE_SPIRV_TO_RELOCATABLE:
case AMD_COMGR_ACTION_TRANSLATE_SPIRV_TO_BC:
case AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV:
ActionStatus = dispatchCompilerAction(ActionKind, ActionInfoP, InputSetP,
ResultSetP, *LogP);
break;
Expand Down
1 change: 1 addition & 0 deletions amd/comgr/test-lit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ add_comgr_lit_binary(source-to-bc-with-dev-libs c)
add_comgr_lit_binary(spirv-translator c)
add_comgr_lit_binary(compile-opencl-minimal c)
add_comgr_lit_binary(spirv-to-reloc c)
add_comgr_lit_binary(source-to-spirv c)
add_comgr_lit_binary(unbundle c)
add_comgr_lit_binary(get-version c)
add_comgr_lit_binary(status-string c)
Expand Down
55 changes: 55 additions & 0 deletions amd/comgr/test-lit/comgr-sources/source-to-spirv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===- source-to-spirv.c --------------------------------------------------===//
//
// Part of Comgr, under the Apache License v2.0 with LLVM Exceptions. See
// amd/comgr/LICENSE.TXT in this repository for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "amd_comgr.h"
#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
char *BufSource;
size_t SizeSource;
amd_comgr_data_t DataSource;
amd_comgr_data_set_t DataSetSource, DataSetSpirv;
amd_comgr_action_info_t DataAction;
size_t Count;

if (argc != 3) {
fprintf(stderr, "Usage: source-to-spirv file.hip file.spv\n");
exit(1);
}

SizeSource = setBuf(argv[1], &BufSource);

amd_comgr_(create_data(AMD_COMGR_DATA_KIND_SOURCE, &DataSource));
amd_comgr_(set_data(DataSource, SizeSource, BufSource));
amd_comgr_(set_data_name(DataSource, "file.hip"));

amd_comgr_(create_data_set(&DataSetSource));
amd_comgr_(data_set_add(DataSetSource, DataSource));

amd_comgr_(create_action_info(&DataAction));
amd_comgr_(action_info_set_language(DataAction, AMD_COMGR_LANGUAGE_HIP));

amd_comgr_(create_data_set(&DataSetSpirv));
amd_comgr_(do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_TO_SPIRV,
DataAction, DataSetSource, DataSetSpirv));

amd_comgr_data_t DataSpirv;
amd_comgr_(action_data_get_data(DataSetSpirv, AMD_COMGR_DATA_KIND_SPIRV,
0, &DataSpirv));
dumpData(DataSpirv, argv[2]);

amd_comgr_(release_data(DataSource));
amd_comgr_(destroy_data_set(DataSetSource));
amd_comgr_(destroy_data_set(DataSetSpirv));
amd_comgr_(destroy_action_info(DataAction));
free(BufSource);
}