[llvm] [IPSCCP] Variable not visible at Og: (PR #66745)

Felipe de Azevedo Piovezan via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 19 08:35:35 PDT 2023


================
@@ -371,6 +372,52 @@ static bool runIPSCCP(
       StoreInst *SI = cast<StoreInst>(GV->user_back());
       SI->eraseFromParent();
     }
+
+    // Try to create a debug constant expression for the glbal variable
+    // initializer value.
+    SmallVector<DIGlobalVariableExpression *, 1> GVEs;
+    GV->getDebugInfo(GVEs);
+    if (GVEs.size() == 1) {
+      DIBuilder DIB(M);
+
+      // Create integer constant expression.
+      auto createIntExpression = [&DIB](const Constant *CV) -> DIExpression * {
+        const APInt &API = dyn_cast<ConstantInt>(CV)->getValue();
+        std::optional<uint64_t> InitIntOpt;
+        if (API.isNonNegative())
+          InitIntOpt = API.tryZExtValue();
+        else if (auto Temp = API.trySExtValue(); Temp.has_value())
+          // Transform a signed optional to unsigned optional.
+          InitIntOpt = (uint64_t)Temp.value();
+        return DIB.createConstantValueExpression(InitIntOpt.value());
----------------
felipepiovezan wrote:

This is unconditionally calling `std::optional::value`, but the control flow above makes it look like we may have a `nullopt` here. This would be even more evident if we had not used an uninitialized variable here (it would be evident that we are missing an initialization case).

I think we need to account for the possibility of failure here:

```
DIExpression* createIntExpression(DIB, CV) {
        const APInt &API = dyn_cast<ConstantInt>(CV)->getValue();
        if (API.isNonNegative()) {
           if(optional<uint64_t> MaybeZExt = API.tryZExtValue())
                   return DIB.createConstantValueExpression(*MaybeZExt);
        }
        else if (optional<uint64_t> MaybeSExt = API.tryZExtValue())
                          return DIB.createConstantValueExpression(* MaybeSExt);
       return nullptr;
}; 

https://github.com/llvm/llvm-project/pull/66745


More information about the llvm-commits mailing list