[llvm] DXIL: Use correct type ID when writing ValueAsMetadata. (PR #94337)

Tim Besard via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 4 04:14:48 PDT 2024


https://github.com/maleadt created https://github.com/llvm/llvm-project/pull/94337

Small drive-by PR (I'm not working on DXIL, but am working on a similar IR downgrader based on the opaque pointer handling developed for DXIL): It looks like the `ValueAsMetadata` handling currently is broken when writing references to functions. For example:

```llvm
target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
target triple = "dxil-unknown-shadermodel6.7-library"

define void @kernel(ptr addrspace(1)) {
    ret void
}

!kernel = !{!0}
!0 = !{ptr @kernel}
```

Assembling this with `llc` and disassembling the contained IR with `llvm-dis` from LLVM 12 (again, I'm not working on DXIL so I don't have `dxil-dis` handy) breaks:

```
Assertion failed: (V && "Unexpected null Value"), function get, file Metadata.cpp, line 350.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /Users/tim/Julia/src/llvm-12/build/bin/llvm-dis reduced.dxil -o -
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  llvm-dis                 0x0000000102f421bc llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 80
1  llvm-dis                 0x0000000102f42720 PrintStackTraceSignalHandler(void*) + 28
2  llvm-dis                 0x0000000102f40890 llvm::sys::RunSignalHandlers() + 140
3  llvm-dis                 0x0000000102f43c14 SignalHandler(int) + 276
4  libsystem_platform.dylib 0x000000018611f584 _sigtramp + 56
5  libsystem_pthread.dylib  0x00000001860eec20 pthread_kill + 288
6  libsystem_c.dylib        0x0000000185ffba30 abort + 180
7  libsystem_c.dylib        0x0000000185ffad20 err + 0
8  llvm-dis                 0x0000000102ce8db4 llvm::ValueAsMetadata::get(llvm::Value*) + 100
9  llvm-dis                 0x00000001029d672c llvm::MetadataLoader::MetadataLoaderImpl::parseOneMetadata(llvm::SmallVectorImpl<unsigned long long>&, unsigned int, (anonymous namespace)::(anonymous namespace)::PlaceholderQueue&, llvm::StringRef, unsigned int&) + 2184
10 llvm-dis                 0x00000001029d5a78 llvm::MetadataLoader::MetadataLoaderImpl::parseMetadata(bool) + 1448
11 llvm-dis                 0x00000001029df7fc llvm::MetadataLoader::parseMetadata(bool) + 60
12 llvm-dis                 0x000000010298634c llvm::MetadataLoader::parseModuleMetadata() + 40
13 llvm-dis                 0x0000000102982f2c (anonymous namespace)::BitcodeReader::parseModule(unsigned long long, bool, llvm::function_ref<llvm::Optional<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > (llvm::StringRef)>) + 2272
```

IIUC, this is because the value emitted here is of type `typedptr(void (ptr addrspace(1)), 0)`, while the type is simply `void (i8*)*` because it's not looked up in the PointerAnalysisMap. Passing `V` along fixes this.

cc @bogner @llvm-beanz

>From 53f3084b1bb88eaabd017aa79e429e77c24a52f9 Mon Sep 17 00:00:00 2001
From: Tim Besard <tim.besard at gmail.com>
Date: Tue, 4 Jun 2024 13:05:50 +0200
Subject: [PATCH] DXIL: Use correct type ID when writing ValueAsMetadata.

---
 llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
index bc7637a3e3def..054bcd5eaf42e 100644
--- a/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
+++ b/llvm/lib/Target/DirectX/DXILWriter/DXILBitcodeWriter.cpp
@@ -1348,7 +1348,7 @@ void DXILBitcodeWriter::writeValueAsMetadata(
     Ty = TypedPointerType::get(F->getFunctionType(), F->getAddressSpace());
   else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
     Ty = TypedPointerType::get(GV->getValueType(), GV->getAddressSpace());
-  Record.push_back(getTypeID(Ty));
+  Record.push_back(getTypeID(Ty, V));
   Record.push_back(VE.getValueID(V));
   Stream.EmitRecord(bitc::METADATA_VALUE, Record, 0);
   Record.clear();



More information about the llvm-commits mailing list