<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57862>57862</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[C++20] [Modules] [NVPTX] Modules won't work for the NVPTX target
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
aaronmondal
</td>
</tr>
</table>
<pre>
There should be some way to make modules work for heterogeneous code. Being forced to somehow use host-only headers in GMFs and completely separate device-side code from modularized code is too much of a limitation. Especially since heterogeneous compilations could benefit significantly from modules in general.
Since we are compiling for different targets, we need to precompile interface units twice - one host and one device pass. The host pass works fine, but the device pass won't, raising `Module has a nontrivial global ctor, which NVPTX does not support.`:
```bash
echo "export module hello;" > hello.cppm
echo "import hello;" > main.cpp
echo "Building host pcm."
clang -xcuda -nocudalib -nocudainc -std=c++20 --offload-host-only -Xclang -emit-module-interface --precompile hello.cppm -o host.pcm
echo "Building host interface object."
clang -std=c++20 --target=x86_64 --offload-host-only host.pcm -c -o host.interface.o
echo "Building main for host."
clang -xcuda -nocudalib -nocudainc -std=c++20 --offload-host-only -fmodule-file=host.pcm main.cpp -c -o main_device.o
echo "Building device pcm."
clang -xcuda -nocudalib -nocudainc -std=c++20 --offload-device-only -Xclang -emit-module-interface --precompile hello.cppm -o device.pcm
echo "Building device interface object."
clang -std=c++20 --target=nvptx --offload-device-only device.pcm -c -o device.interface.o
echo "The above won't work. Here is the bitcode with the global ctor:"
clang -std=c++20 --target=nvptx --offload-device-only device.pcm -c -o device.interface.bc -emit-llvm
llvm-dis device.interface.bc -o -
echo "Building main for device."
clang -xcuda -nocudalib -nocudainc -std=c++20 --offload-device-only -fmodule-file=device.pcm main.cpp -c -o main_host.o
```
Note that using `--precompile` without `-Xclang -emit-module-interface` will just run the preprocessor and not generate an actual BMI with `-xcuda`.
I'm not too famililar with LLVM bitcode, but it seems to me that this doesn't work for NVPTX:
```llvm
@_ZGIW5hello__in_chrg = internal global i8 0, align 1
@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_ZGIW5hello, ptr null }]
define void @_ZGIW5hello() #0 {
%1 = load i8, ptr @_ZGIW5hello__in_chrg, align 1
%2 = icmp eq i8 %1, 0
br i1 %2, label %3, label %4, !prof !4
3: ; preds = %0
store i8 1, ptr @_ZGIW5hello__in_chrg, align 1
br label %4
4: ; preds = %3, %0
ret void
}
```
The `Module` constructor is here:
https://github.com/llvm/llvm-project/blob/42bcb35c0f2946b66ae397515823a3792f14b067/llvm/lib/IR/Module.cpp#L72-L76
The error message is raised here, and it looks like there should be a way to not trigger the error for some other kind of module configuration:
https://github.com/llvm/llvm-project/blob/636de2bf342ebae9a1d4ccb9c0029e72c7ce6e26/llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp#L777-L799
cc @Artem-B @ChuanqiXu9
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9V9tu2zgQ_Rr5hZAhS7ZsPfihbppugKQodoNusS8BRY0sNhKpklSS7tfvDCnZzq2Lot0NjEgk53J4ZjhDlbr6tr1uwACzjR7aipX4pjtg9_wbc5p1_BZYp6uhBcvutblltTasAQdG70GBHiwTuoI524FUe1oVUJEmWWn0PRsssEZbF2vVfkNNXoGxTCr2_urcMq4q1O_6Fi3isoWeG-6AVXAnBcRWVuDts9roLgDhRv4NVZiVFl0hykE0TNeMs1Z20nEntZqzd7YHIXlLdqUS8Ax218vWy9IgbF5BLR2K75WspeDKofLRNXjgZMDwdh4lZ1HyJvz_wzu4B8YNjJZHOlgl6xoJVo45bvbgbJS-JUkFgajeQFDA7SgEWHO0NCjpcG_3SAKLmVaBQ08XDQI9rOfWzhnGL6zS0AfJsloqID_lgG6bRwoooaJ07WjZcGkJZ5QnV36DrOEYFKa0ckbeIXls3-oSH8Jp44E3Ern-8Onj9WdWaSREaeRr6Htt3BzNRNnICL37X8ltE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