|
| 1 | +from os import remove |
| 2 | +from subprocess import check_call |
| 3 | +from tempfile import NamedTemporaryFile |
| 4 | + |
| 5 | +from dpcpp_llvm_spirv import get_llvm_spirv_path |
| 6 | + |
| 7 | +test_ll = """source_filename = "<string>" |
| 8 | +target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024" |
| 9 | +target triple = "spir64-unknown-unknown" |
| 10 | +
|
| 11 | +define i32 @mult(i32 %a, i32 %b) #0 { |
| 12 | + %1 = mul nsw i32 %a, %b |
| 13 | + ret i32 %1 |
| 14 | +}""" |
| 15 | + |
| 16 | +expected_spvasm = """; SPIR-V |
| 17 | +; Version: 1.0 |
| 18 | +; Generator: Khronos LLVM/SPIR-V Translator; 14 |
| 19 | +; Bound: 9 |
| 20 | +; Schema: 0 |
| 21 | + OpCapability Addresses |
| 22 | + OpCapability Linkage |
| 23 | + OpCapability Kernel |
| 24 | + %1 = OpExtInstImport "OpenCL.std" |
| 25 | + OpMemoryModel Physical64 OpenCL |
| 26 | + OpSource Unknown 0 |
| 27 | + OpName %mult "mult" |
| 28 | + OpName %a "a" |
| 29 | + OpName %b "b" |
| 30 | + OpDecorate %mult LinkageAttributes "mult" Export |
| 31 | + %uint = OpTypeInt 32 0 |
| 32 | + %3 = OpTypeFunction %uint %uint %uint |
| 33 | + %mult = OpFunction %uint None %3 |
| 34 | + %a = OpFunctionParameter %uint |
| 35 | + %b = OpFunctionParameter %uint |
| 36 | + %7 = OpLabel |
| 37 | + %8 = OpIMul %uint %a %b |
| 38 | + OpReturnValue %8 |
| 39 | + OpFunctionEnd |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +def main(): |
| 44 | + ll_file = NamedTemporaryFile(prefix="multiply", suffix=".ll") |
| 45 | + ll_file.write(test_ll.encode()) |
| 46 | + ll_file.flush() |
| 47 | + |
| 48 | + name = ll_file.name[:-3] |
| 49 | + |
| 50 | + check_call(["llvm-as", name + ".ll", "-o", name + ".bc"]) |
| 51 | + check_call( |
| 52 | + [ |
| 53 | + get_llvm_spirv_path(), |
| 54 | + "-spirv-max-version=1.0", |
| 55 | + name + ".bc", |
| 56 | + "-o", |
| 57 | + name + ".spv", |
| 58 | + ] |
| 59 | + ) |
| 60 | + check_call(["spirv-dis", name + ".spv", "-o", name + ".spvasm"]) |
| 61 | + |
| 62 | + spv_file = open(name + ".spvasm", "r") |
| 63 | + |
| 64 | + got_spvasm = spv_file.read() |
| 65 | + |
| 66 | + assert got_spvasm == expected_spvasm |
| 67 | + |
| 68 | + # close file and remove all temp files |
| 69 | + spv_file.close() |
| 70 | + ll_file.close() |
| 71 | + remove(name + ".bc") |
| 72 | + remove(name + ".spv") |
| 73 | + remove(name + ".spvasm") |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + main() |
0 commit comments