================
@@ -4275,8 +4275,15 @@ static void emitGlobalConstantImpl(const DataLayout &DL, 
const Constant *CV,
       return emitGlobalConstantFP(CFP, AP);
   }
 
-  if (isa<ConstantPointerNull>(CV)) {
-    AP.OutStreamer->emitIntValue(0, Size);
+  if (auto *NullPtr = dyn_cast<ConstantPointerNull>(CV)) {
+    if (std::optional<APInt> NullPtrVal =
+            DL.getNullPtrValue(NullPtr->getType()->getPointerAddressSpace())) {
+      AP.OutStreamer->emitIntValue(NullPtrVal->getSExtValue(), Size);
+    } else {
+      // We fall back to the default behavior of emitting a zero value if we
+      // can't get the null pointer value from the data layout.
+      AP.OutStreamer->emitIntValue(0, Size);
+    }
----------------
arichardson wrote:

```suggestion
     APInt NullPtrVal = 
DL.getNullPtrValue(NullPtr->getType()->getPointerAddressSpace());
    AP.OutStreamer->emitIntValue(NullPtrVal.getSExtValue(), Size);
```

I wonder if we should drop the std::optional from the DataLayout and just 
always have a defined value? We could then also either drop the 'c' flag or do 
something like `pc{0xaaaaa}3:64:64` to define a AS3 pointer with nullptr value 
of 0xaaaa?

Silently falling back to zero here seems like it would cause more pain for 
hypothetical targets with custom null pointers than forcing them to set the 
datalayout?

https://github.com/llvm/llvm-project/pull/166667
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to