[clang] [lld] [llvm] [WIP][IR][Constants] Change the semantic of `ConstantPointerNull` to represent an actual `nullptr` instead of a zero-value pointer (PR #166667)

Alexander Richardson via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 17 10:47:21 PST 2025


================
@@ -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


More information about the llvm-commits mailing list