[clang] [CIR] Upstream initial support for union type (PR #137501)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 28 06:56:51 PDT 2025


================
@@ -230,17 +230,41 @@ void RecordType::complete(ArrayRef<Type> members, bool packed, bool padded) {
     llvm_unreachable("failed to complete record");
 }
 
+/// Return the largest member of in the type.
+///
+/// Recurses into union members never returning a union as the largest member.
+Type RecordType::getLargestMember(const ::mlir::DataLayout &dataLayout) const {
+  assert(isUnion() && "Only call getLargestMember on unions");
+  Type largestMember;
+  unsigned largestMemberSize = 0;
+  unsigned numElements = getNumElements();
+  auto members = getMembers();
+  if (getPadded())
+    numElements -= 1; // The last element is padding.
+  for (unsigned i = 0; i < numElements; ++i) {
----------------
erichkeane wrote:

This loop looks to me to be trivially a `std::max_element`, which would end up saving a lot of the work here.  

Either way, that `if` statement is really difficult to decode, and we should see if we can do better here.


Also, could probably use `make_range` to use a range-for here to make this easier to read as well.

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


More information about the cfe-commits mailing list