[clang] [clang-repl] Fix TargetMachine leak in CUDA device PTX generation (PR #228208)
Aaron Jomy via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 1 12:54:13 PDT 2026
aaronj0 wrote:
Follow-up to https://github.com/llvm/llvm-project/pull/226975: the sanitizer bots failed on the added unittest, see https://lab.llvm.org/buildbot/#/builders/55/builds/33928:
```
==ClangReplInterpreterTests==1819891==ERROR: LeakSanitizer: detected memory leaks
Indirect leak of 514328 byte(s) in 1 object(s) allocated from:
#0 0xabfb71844c20 in operator new(unsigned long) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/compiler-rt/lib/hwasan/hwasan_new_delete.cpp:122:3
#1 0xabfb74672ef4 in llvm::RegisterTargetMachine<llvm::NVPTXTargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, std::__1::optional<llvm::Reloc::Model>, std::__1::optional<llvm::CodeModel::Model>, llvm::CodeGenOptLevel, bool) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/MC/TargetRegistry.h:1288:12
#2 0xabfb78c0b980 in createTargetMachine /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/llvm/include/llvm/MC/TargetRegistry.h:475:12
#3 0xabfb78c0b980 in clang::IncrementalCUDADeviceParser::GeneratePTX() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Interpreter/DeviceOffload.cpp:56:48
#4 0xabfb78bf064c in clang::Interpreter::Parse(llvm::StringRef) /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/lib/Interpreter/Interpreter.cpp:582:57
#5 0xabfb7189dd54 in (anonymous namespace)::DeviceOffloadTest_FirstDeviceModuleVerifies_Test::TestBody() /home/b/sanitizer-aarch64-linux-bootstrap-hwasan/build/llvm-project/clang/unittests/Interpreter/DeviceOffloadTest.cpp:67:32
...
```
I tried reproducing locally with an ASan build:
```
[----------] 1 test from DeviceOffloadTest
[ RUN ] DeviceOffloadTest.FirstDeviceModuleVerifies
/home/maximus/llvm-dev/wt-asan/clang/unittests/Interpreter/DeviceOffloadTe
Death test: { IncrementalCompilerBuilder CB; CB.SetCompilerArgs( {"-nocudainc", "-nocudalib", "-fverify-intermediate-code"}); auto DeviceCI =
llvm::cantFail(CB.CreateDevice(OffloadType::CUDA)); auto HostCI = llvm::caype::CUDA)); auto Interp = llvm::cantFail(Interpreter::createWithDevice(OffloadType::CUDA, std::move(HostCI), std::move(DeviceCI))); llvm::cantFail(Interp->Parse("__attribute__((device)) void f() {}")); exit(0); }
Result: died but not with expected exit code:
Exited with exit status 1
Actual msg:
[ DEATH ]
[ DEATH ] =============================================================
[ DEATH ] ==1453170==ERROR: LeakSanitizer: detected memory leaks
[ DEATH ]
[ DEATH ] Indirect leak of 514576 byte(s) in 1 object(s) allocated from:
[ DEATH ] #0 0x73b0ad6fe548 in operator new(unsigned long) ../../..n_new_delete.cpp:95
[ DEATH ] #1 0x574c28df5546 in llvm::RegisterTargetMachine<llvm::NVPTXTargetMachine>::Allocator(llvm::Target const&, llvm::Triple const&, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, std::optional<llvm::Reloc::Model>, std::optionlvm::CodeGenOptLevel, bool)(/home/maximus/llvm-dev/wt-asan/build/tools/clang/unittests/Interpreter/ClangReplInterpreterTests+0xefe546) (BuildId: 1e83ecc39d1883f21ce500833ada23601b019e39)
[ DEATH ] #2 0x574c2f4a3b78 in clang::IncrementalCUDADeviceParser::(/home/maximus/llvm-dev/wt-asan/build/tools/clang/unittests/Interpreter/ClangReplInterpreterTests+0x75acb78) (BuildId: 1e83ecc39d1883f21ce500833ada23601b019e39)
[ DEATH ] #3 0x574c2f480a49 in clang::Interpreter::Parse(llvm::Stri(/home/maximus/llvm-dev/wt-asan/build/tools/clang/unittests/Interpreter/ClangReplInterpreterTests+0x7589a49) (BuildId: 1e83ecc39d1883f21ce500833ada23601b019e39)
[ DEATH ] #4 0x574c28c49817 in (anonymous namespace)::DeviceOffloads_Test::TestBody()(/home/maximus/llvm-dev/wt-asan/build/tools/clang/unittests/Interpreter/ClangReplInterpreterTests+0xd52817) (BuildId: 1e83ecc39d1883f21ce500833ada23601b019e39)
[ DEATH ] #5 0x574c2c1db314 in void testing::internal::HandleExceptng::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)[clone .isra.0] (/home/maximus/llvm-dev/wt-asan/build/tools/clang/unittests/Interpreter/ClangReplInterpreterTests+0x42e4314) (BuildId: 1e83ecc39d1883f21ce500833ada23601b019e39)
...
[ DEATH ] SUMMARY: AddressSanitizer: 521880 byte(s) leaked in 46 allocation(s).
[ DEATH ]
[ FAILED ] DeviceOffloadTest.FirstDeviceModuleVerifies (181 ms)
[----------] 1 test from DeviceOffloadTest (181 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (181 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] DeviceOffloadTest.FirstDeviceModuleVerifies
1 FAILED TEST
```
All 46 allocations are the NVPTX TargetMachine and its owned objects. With this patch:
```
$ ./tools/clang/unittests/Interpreter/ClangReplInterpreterTests --gtest_filter=DeviceOffloadTest.*
Note: Google Test filter = DeviceOffloadTest.*
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from DeviceOffloadTest
[ RUN ] DeviceOffloadTest.FirstDeviceModuleVerifies
[ OK ] DeviceOffloadTest.FirstDeviceModuleVerifies (70 ms)
[----------] 1 test from DeviceOffloadTest (70 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (71 ms total)
[ PASSED ] 1 test.
```
and the ClangReplInterpreterTests binary runs cleanly.
https://github.com/llvm/llvm-project/pull/228208
More information about the cfe-commits
mailing list