Skip to content

Commit acd3795

Browse files
lamb-jsearlmc1
authored andcommitted
[Comgr] Add LIT testing capabilites to Comgr
To robustly test Comgr, we can leverage LLVM tools like llvm-dis, llvm-link, lld, etc. To facilitate this, in this patch we set up the infrastructure for Comgr llvm-lit tests. These tests can be added in the comgr/test-lit directory, and can be run via: make test-lit They are also added to the check-comgr target, so they can be run along with the traditional tests with make check-comgr Change-Id: I18fafd09c1ea280d78d32071e6877868cfaa66e5
1 parent 489c297 commit acd3795

File tree

8 files changed

+293
-0
lines changed

8 files changed

+293
-0
lines changed

amd/comgr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ if(BUILD_TESTING)
448448
set_property(GLOBAL APPEND PROPERTY LLVM_ADDITIONAL_TEST_TARGETS check-comgr)
449449
endif()
450450
add_subdirectory(test)
451+
add_subdirectory(test-lit)
451452
endif()
452453

453454
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)

amd/comgr/test-lit/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
configure_file(lit.site.cfg.py.in lit.site.cfg.py @ONLY)
2+
3+
# Comgr source build
4+
if (EXISTS "${LLVM_TOOLS_BINARY_DIR}/../../bin/llvm-lit")
5+
set(LLVM_LIT_PATH "${LLVM_TOOLS_BINARY_DIR}/../../bin/llvm-lit")
6+
# LLVM external projects build
7+
else()
8+
set(LLVM_LIT_PATH "${LLVM_TOOLS_BINARY_DIR}/llvm-lit")
9+
endif()
10+
11+
add_custom_target(test-lit COMMAND "${LLVM_LIT_PATH}"
12+
"${CMAKE_CURRENT_BINARY_DIR}" -v)
13+
14+
macro(add_comgr_lit_binary name)
15+
add_executable("${name}" "comgr-sources/${name}.c")
16+
set_target_properties("${name}" PROPERTIES
17+
C_STANDARD 99
18+
C_STANDARD_REQUIRED Yes
19+
C_EXTENSIONS No)
20+
target_link_libraries("${name}" amd_comgr)
21+
add_dependencies(check-comgr "${name}")
22+
endmacro()
23+
24+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
25+
26+
add_comgr_lit_binary(source-to-bc-with-dev-libs)
27+
28+
add_dependencies(check-comgr test-lit)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#ifndef COMGR_TEST_COMMON_H
2+
#define COMGR_TEST_COMMON_H
3+
4+
#include "amd_comgr.h"
5+
#include <inttypes.h>
6+
#include <stdarg.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <string.h>
10+
11+
#if !defined(_WIN32) && !defined(_WIN64)
12+
#include <sys/stat.h>
13+
#include <sys/types.h>
14+
#include <sys/syscall.h>
15+
#include <unistd.h>
16+
#else // Windows
17+
#include <io.h>
18+
#endif
19+
#include <errno.h>
20+
#include <fcntl.h>
21+
22+
#define amd_comgr_(call) \
23+
do { \
24+
amd_comgr_status_t status = amd_comgr_ ## call; \
25+
if (status != AMD_COMGR_STATUS_SUCCESS) { \
26+
const char* reason = ""; \
27+
amd_comgr_status_string(status, &reason); \
28+
fail(#call " failed: %s\n file, line: %s, %d\n", \
29+
reason, __FILE__, __LINE__); \
30+
} \
31+
} while (false)
32+
33+
static void fail(const char *format, ...) {
34+
va_list ap;
35+
va_start(ap, format);
36+
37+
printf("FAILED: ");
38+
vprintf(format, ap);
39+
printf("\n");
40+
41+
va_end(ap);
42+
43+
exit(1);
44+
}
45+
46+
static int setBuf(const char *infile, char **buf) {
47+
FILE *fp;
48+
long size;
49+
50+
fp = fopen(infile, "rb");
51+
if (!fp)
52+
fail("fopen : %s", infile);
53+
if (fseek(fp, 0L, SEEK_END) != 0)
54+
fail("fopen");
55+
size = ftell(fp);
56+
if (size == -1)
57+
fail("ftell");
58+
if (fseek(fp, 0, SEEK_SET) != 0)
59+
fail("fseek");
60+
61+
*buf = (char *) malloc(size + 1);
62+
if (!*buf)
63+
fail("malloc");
64+
if (fread(*buf, size, 1, fp) != 1)
65+
fail("fread");
66+
if (fclose(fp) != 0)
67+
fail("fclose");
68+
(*buf)[size] = 0; // terminating zero
69+
return size;
70+
}
71+
72+
static void dumpData(amd_comgr_data_t Data, const char *OutFile) {
73+
size_t size;
74+
char *bytes = NULL;
75+
amd_comgr_status_t status;
76+
77+
amd_comgr_(get_data(Data, &size, NULL));
78+
79+
bytes = (char *)malloc(size);
80+
if (!bytes)
81+
fail("malloc");
82+
83+
amd_comgr_(get_data(Data, &size, bytes));
84+
85+
FILE *fp = fopen(OutFile, "wb");
86+
if (!fp)
87+
fail("fopen : %s", OutFile);
88+
89+
size_t ret = fwrite(bytes, sizeof(char), size, fp);
90+
if (ret != size)
91+
fail("fwrite");
92+
93+
free(bytes);
94+
fclose(fp);
95+
}
96+
97+
#endif // COMGR_TEST_COMMON_H
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "amd_comgr.h"
2+
#include "common.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
int main(int argc, char *argv[]) {
8+
char *BufSource;
9+
size_t SizeSource;
10+
amd_comgr_data_t DataSource;
11+
amd_comgr_data_set_t DataSetIn, DataSetPch, DataSetBc, DataSetLinked,
12+
DataSetReloc, DataSetExec;
13+
amd_comgr_action_info_t DataAction;
14+
const char *CodeGenOptions[] = {
15+
"-mcode-object-version=5", "-mllvm", "-amdgpu-prelink"};
16+
size_t CodeGenOptionsCount =
17+
sizeof(CodeGenOptions) / sizeof(CodeGenOptions[0]);
18+
if (argc != 4) {
19+
fprintf(stderr, "Usage: source-to-bc-with-device-libs file.cl -o file.bc\n");
20+
exit(1);
21+
}
22+
23+
SizeSource = setBuf(argv[1], &BufSource);
24+
25+
amd_comgr_(create_data_set(&DataSetIn));
26+
amd_comgr_(create_data(AMD_COMGR_DATA_KIND_SOURCE, &DataSource));
27+
amd_comgr_(set_data(DataSource, SizeSource, BufSource));
28+
amd_comgr_(set_data_name(DataSource, "device-lib-linking.cl"));
29+
amd_comgr_(data_set_add(DataSetIn, DataSource));
30+
31+
amd_comgr_(create_action_info(&DataAction));
32+
amd_comgr_(action_info_set_language(DataAction,
33+
AMD_COMGR_LANGUAGE_OPENCL_1_2));
34+
amd_comgr_(action_info_set_isa_name(DataAction, "amdgcn-amd-amdhsa--gfx900"));
35+
amd_comgr_(create_data_set(&DataSetPch));
36+
37+
amd_comgr_(do_action(AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS, DataAction,
38+
DataSetIn, DataSetPch));
39+
40+
size_t Count;
41+
amd_comgr_(action_data_count(DataSetPch,
42+
AMD_COMGR_DATA_KIND_PRECOMPILED_HEADER, &Count));
43+
44+
if (Count != 1) {
45+
printf("AMD_COMGR_ACTION_ADD_PRECOMPILED_HEADERS Failed: "
46+
"produced %zu precompiled header objects (expected 1)\n",
47+
Count);
48+
exit(1);
49+
}
50+
51+
amd_comgr_(create_data_set(&DataSetBc));
52+
amd_comgr_(action_info_set_option_list(DataAction, CodeGenOptions,
53+
CodeGenOptionsCount));
54+
amd_comgr_(do_action(AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC,
55+
DataAction, DataSetPch, DataSetBc));
56+
57+
amd_comgr_(action_data_count(DataSetBc, AMD_COMGR_DATA_KIND_BC, &Count));
58+
59+
if (Count != 1) {
60+
printf("AMD_COMGR_ACTION_COMPILE_SOURCE_TO_BC Failed: "
61+
"produced %zu BC objects (expected 1)\n",
62+
Count);
63+
exit(1);
64+
}
65+
66+
amd_comgr_data_t DataBc;
67+
amd_comgr_(action_data_get_data(DataSetBc, AMD_COMGR_DATA_KIND_BC, 0,
68+
&DataBc));
69+
dumpData(DataBc, argv[3]);
70+
71+
amd_comgr_(release_data(DataSource));
72+
amd_comgr_(release_data(DataBc));
73+
amd_comgr_(destroy_data_set(DataSetIn));
74+
amd_comgr_(destroy_data_set(DataSetPch));
75+
amd_comgr_(destroy_data_set(DataSetBc));
76+
amd_comgr_(destroy_action_info(DataAction));
77+
free(BufSource);
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// COM: Run Comgr binary to compile OpenCL source into LLVM IR Bitcode, linking
2+
// COM: against the AMD Device Libraries
3+
// RUN: source-to-bc-with-dev-libs %s -o %t-with-dev-libs.bc
4+
5+
// COM: Dissasemble LLVM IR bitcode to LLVM IR text
6+
// RUN: llvm-dis %t-with-dev-libs.bc -o - | FileCheck %s
7+
8+
// COM: Verify LLVM IR text file
9+
// CHECK: target triple = "amdgcn-amd-amdhsa"
10+
// CHECK: define internal float @_Z4powrff
11+
// CHECK: define internal float @_Z6sincosfPU3AS5f
12+
// CHECK: define internal float @_Z4cbrtf
13+
// CHECK: define internal float @__ocml_sincos_f32
14+
// CHECK: define internal float @__ocml_powr_f32
15+
// CHECK: define internal noundef float @__ocml_exp_f32
16+
// CHECK: define internal ptr addrspace(1) @__printf_alloc
17+
18+
extern const __constant bool __oclc_finite_only_opt;
19+
extern const __constant bool __oclc_unsafe_math_opt;
20+
extern const __constant bool __oclc_correctly_rounded_sqrt32;
21+
extern const __constant bool __oclc_wavefrontsize64;
22+
extern const __constant int __oclc_ISA_version;
23+
extern const __constant int __oclc_ABI_version;
24+
25+
void kernel device_libs(__global float *status) {
26+
27+
if (__oclc_finite_only_opt) status[0] = 1.0;
28+
if (__oclc_unsafe_math_opt) status[1] = 1.0;
29+
if (__oclc_correctly_rounded_sqrt32) status[3] = 1.0;
30+
if (__oclc_wavefrontsize64) status[4] = 1.0;
31+
if (__oclc_ISA_version) status[5] = 1.0;
32+
if (__oclc_ABI_version) status[6] = 1.0;
33+
34+
// Math functions to test AMDGPULibCalls Folding optimizations
35+
// fold_sincos()
36+
float x = 0.25;
37+
status[7] = sin(x) + cos(x);
38+
status[8] = cos(x) + sin(x);
39+
40+
// fold_rootn()
41+
float y = 725.0;
42+
status[9] = rootn(y, 3);
43+
status[10] = rootn(y, -1);
44+
status[11] = rootn(y, -2);
45+
46+
// fold_pow()
47+
float z = 12.16;
48+
status[12] = pow(z, (float) 0.5);
49+
status[13] = powr(y, (float) 7.23);
50+
51+
// printf()
52+
printf("testy\n");
53+
}

amd/comgr/test-lit/hello-world.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: clang %s -o %t
2+
// RUN: %t | grep -e "hello world"
3+
#include <stdio.h>
4+
5+
int main() {
6+
puts("hello world");
7+
return 0;
8+
}

amd/comgr/test-lit/lit.cfg.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
3+
import lit.formats
4+
import lit.util
5+
6+
config.name = "Comgr"
7+
config.suffixes = {".hip", ".cl", ".c", ".cpp"}
8+
config.test_format = lit.formats.ShTest(True)
9+
10+
config.excludes = ["comgr-sources"]
11+
12+
config.test_source_root = os.path.dirname(__file__)
13+
config.test_exec_root = config.my_obj_root

amd/comgr/test-lit/lit.site.cfg.py.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
config.my_src_root = r'@CMAKE_CURRENT_SOURCE_DIR@'
4+
config.my_obj_root = r'@CMAKE_CURRENT_BINARY_DIR@'
5+
6+
# Needed for clang, llvm-dis, etc.
7+
config.environment['PATH'] = os.pathsep.join(["@LLVM_TOOLS_BINARY_DIR@",
8+
config.environment['PATH']])
9+
10+
# Needed for Comgr binaries
11+
config.environment['PATH'] = os.pathsep.join(["@CMAKE_CURRENT_BINARY_DIR@",
12+
config.environment['PATH']])
13+
14+
lit_config.load_config(
15+
config, os.path.join(config.my_src_root, "lit.cfg.py"))

0 commit comments

Comments
 (0)