Skip to content

[CIR][NFCI] Update RValue class to reflect changes in classic CodeGen #142779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025

Conversation

mmha
Copy link
Contributor

@mmha mmha commented Jun 4, 2025

This changes clang::CIRGen::RValue to look like current clang::CodeGen::RValue which was changed in 84780a/#86923. This should be NFC and is preliminary work for upstreaming builtin function call support.

This changes clang::CIRGen::RValue to look like current clang::CodeGen::RValue which was changed in 84780a. This should be NFC and is preliminary work for upstreaming builtin function call support.
@mmha mmha requested review from erichkeane and andykaylor June 4, 2025 14:04
@mmha mmha requested review from lanza and bcardosolopes as code owners June 4, 2025 14:05
@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Jun 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 4, 2025

@llvm/pr-subscribers-clangir

Author: Morris Hafner (mmha)

Changes

This changes clang::CIRGen::RValue to look like current clang::CodeGen::RValue which was changed in 84780a/#86923. This should be NFC and is preliminary work for upstreaming builtin function call support.


Full diff: https://github.com/llvm/llvm-project/pull/142779.diff

1 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenValue.h (+70-15)
diff --git a/clang/lib/CIR/CodeGen/CIRGenValue.h b/clang/lib/CIR/CodeGen/CIRGenValue.h
index 56177c948df94..208247e16e531 100644
--- a/clang/lib/CIR/CodeGen/CIRGenValue.h
+++ b/clang/lib/CIR/CodeGen/CIRGenValue.h
@@ -19,8 +19,6 @@
 #include "clang/AST/CharUnits.h"
 #include "clang/AST/Type.h"
 
-#include "llvm/ADT/PointerIntPair.h"
-
 #include "mlir/IR/Value.h"
 
 #include "clang/CIR/MissingFeatures.h"
@@ -34,28 +32,85 @@ namespace clang::CIRGen {
 class RValue {
   enum Flavor { Scalar, Complex, Aggregate };
 
-  // Stores first value and flavor.
-  llvm::PointerIntPair<mlir::Value, 2, Flavor> v1;
-  // Stores second value and volatility.
-  llvm::PointerIntPair<llvm::PointerUnion<mlir::Value, int *>, 1, bool> v2;
-  // Stores element type for aggregate values.
-  mlir::Type elementType;
+  union {
+    // Stores first and second value.
+    struct {
+      mlir::Value first;
+      mlir::Value second;
+    } vals;
+
+    // Stores aggregate address.
+    Address aggregateAddr;
+  };
+
+  unsigned isVolatile : 1;
+  unsigned flavor : 2;
 
 public:
-  bool isScalar() const { return v1.getInt() == Scalar; }
-  bool isAggregate() const { return v1.getInt() == Aggregate; }
+  RValue() : vals{nullptr, nullptr}, flavor(Scalar) {}
+
+  bool isScalar() const { return flavor == Scalar; }
+  bool isComplex() const { return flavor == Complex; }
+  bool isAggregate() const { return flavor == Aggregate; }
+
+  bool isVolatileQualified() const { return isVolatile; }
 
-  /// Return the mlir::Value of this scalar value.
+  /// Return the value of this scalar value.
   mlir::Value getScalarVal() const {
     assert(isScalar() && "Not a scalar!");
-    return v1.getPointer();
+    return vals.first;
+  }
+
+  /// Return the real/imag components of this complex value.
+  std::pair<mlir::Value, mlir::Value> getComplexVal() const {
+    return std::make_pair(vals.first, vals.second);
+  }
+
+  /// Return the value of the address of the aggregate.
+  Address getAggregateAddress() const {
+    assert(isAggregate() && "Not an aggregate!");
+    return aggregateAddr;
+  }
+
+  mlir::Value getAggregatePointer(QualType pointeeType) const {
+    return getAggregateAddress().getPointer();
+  }
+
+  static RValue getIgnored() {
+    // FIXME: should we make this a more explicit state?
+    return get(nullptr);
   }
 
   static RValue get(mlir::Value v) {
     RValue er;
-    er.v1.setPointer(v);
-    er.v1.setInt(Scalar);
-    er.v2.setInt(false);
+    er.vals.first = v;
+    er.flavor = Scalar;
+    er.isVolatile = false;
+    return er;
+  }
+
+  static RValue getComplex(mlir::Value v1, mlir::Value v2) {
+    RValue er;
+    er.vals = {v1, v2};
+    er.flavor = Complex;
+    er.isVolatile = false;
+    return er;
+  }
+  static RValue getComplex(const std::pair<mlir::Value, mlir::Value> &c) {
+    return getComplex(c.first, c.second);
+  }
+  // FIXME: Aggregate rvalues need to retain information about whether they are
+  // volatile or not.  Remove default to find all places that probably get this
+  // wrong.
+
+  /// Convert an Address to an RValue. If the Address is not
+  /// signed, create an RValue using the unsigned address. Otherwise, resign the
+  /// address using the provided type.
+  static RValue getAggregate(Address addr, bool isVolatile = false) {
+    RValue er;
+    er.aggregateAddr = addr;
+    er.flavor = Aggregate;
+    er.isVolatile = isVolatile;
     return er;
   }
 };

@llvmbot
Copy link
Member

llvmbot commented Jun 4, 2025

@llvm/pr-subscribers-clang

Author: Morris Hafner (mmha)

Changes

This changes clang::CIRGen::RValue to look like current clang::CodeGen::RValue which was changed in 84780a/#86923. This should be NFC and is preliminary work for upstreaming builtin function call support.


Full diff: https://github.com/llvm/llvm-project/pull/142779.diff

1 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenValue.h (+70-15)
diff --git a/clang/lib/CIR/CodeGen/CIRGenValue.h b/clang/lib/CIR/CodeGen/CIRGenValue.h
index 56177c948df94..208247e16e531 100644
--- a/clang/lib/CIR/CodeGen/CIRGenValue.h
+++ b/clang/lib/CIR/CodeGen/CIRGenValue.h
@@ -19,8 +19,6 @@
 #include "clang/AST/CharUnits.h"
 #include "clang/AST/Type.h"
 
-#include "llvm/ADT/PointerIntPair.h"
-
 #include "mlir/IR/Value.h"
 
 #include "clang/CIR/MissingFeatures.h"
@@ -34,28 +32,85 @@ namespace clang::CIRGen {
 class RValue {
   enum Flavor { Scalar, Complex, Aggregate };
 
-  // Stores first value and flavor.
-  llvm::PointerIntPair<mlir::Value, 2, Flavor> v1;
-  // Stores second value and volatility.
-  llvm::PointerIntPair<llvm::PointerUnion<mlir::Value, int *>, 1, bool> v2;
-  // Stores element type for aggregate values.
-  mlir::Type elementType;
+  union {
+    // Stores first and second value.
+    struct {
+      mlir::Value first;
+      mlir::Value second;
+    } vals;
+
+    // Stores aggregate address.
+    Address aggregateAddr;
+  };
+
+  unsigned isVolatile : 1;
+  unsigned flavor : 2;
 
 public:
-  bool isScalar() const { return v1.getInt() == Scalar; }
-  bool isAggregate() const { return v1.getInt() == Aggregate; }
+  RValue() : vals{nullptr, nullptr}, flavor(Scalar) {}
+
+  bool isScalar() const { return flavor == Scalar; }
+  bool isComplex() const { return flavor == Complex; }
+  bool isAggregate() const { return flavor == Aggregate; }
+
+  bool isVolatileQualified() const { return isVolatile; }
 
-  /// Return the mlir::Value of this scalar value.
+  /// Return the value of this scalar value.
   mlir::Value getScalarVal() const {
     assert(isScalar() && "Not a scalar!");
-    return v1.getPointer();
+    return vals.first;
+  }
+
+  /// Return the real/imag components of this complex value.
+  std::pair<mlir::Value, mlir::Value> getComplexVal() const {
+    return std::make_pair(vals.first, vals.second);
+  }
+
+  /// Return the value of the address of the aggregate.
+  Address getAggregateAddress() const {
+    assert(isAggregate() && "Not an aggregate!");
+    return aggregateAddr;
+  }
+
+  mlir::Value getAggregatePointer(QualType pointeeType) const {
+    return getAggregateAddress().getPointer();
+  }
+
+  static RValue getIgnored() {
+    // FIXME: should we make this a more explicit state?
+    return get(nullptr);
   }
 
   static RValue get(mlir::Value v) {
     RValue er;
-    er.v1.setPointer(v);
-    er.v1.setInt(Scalar);
-    er.v2.setInt(false);
+    er.vals.first = v;
+    er.flavor = Scalar;
+    er.isVolatile = false;
+    return er;
+  }
+
+  static RValue getComplex(mlir::Value v1, mlir::Value v2) {
+    RValue er;
+    er.vals = {v1, v2};
+    er.flavor = Complex;
+    er.isVolatile = false;
+    return er;
+  }
+  static RValue getComplex(const std::pair<mlir::Value, mlir::Value> &c) {
+    return getComplex(c.first, c.second);
+  }
+  // FIXME: Aggregate rvalues need to retain information about whether they are
+  // volatile or not.  Remove default to find all places that probably get this
+  // wrong.
+
+  /// Convert an Address to an RValue. If the Address is not
+  /// signed, create an RValue using the unsigned address. Otherwise, resign the
+  /// address using the provided type.
+  static RValue getAggregate(Address addr, bool isVolatile = false) {
+    RValue er;
+    er.aggregateAddr = addr;
+    er.flavor = Aggregate;
+    er.isVolatile = isVolatile;
     return er;
   }
 };

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@andykaylor
Copy link
Contributor

Tagging @AmrDeveloper for awareness of the complex value support added here.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bcardosolopes bcardosolopes changed the title [CIR] Update RValue class to reflect changes in classic CodeGen [CIR][NFCI] Update RValue class to reflect changes in classic CodeGen Jun 4, 2025
@mmha mmha merged commit af82e50 into llvm:main Jun 5, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants