https://github.com/HendrikHuebner updated 
https://github.com/llvm/llvm-project/pull/169963

From a915e7685167882f0cc24174b0d4f505ef078e24 Mon Sep 17 00:00:00 2001
From: hhuebner <[email protected]>
Date: Fri, 28 Nov 2025 23:41:30 +0100
Subject: [PATCH 1/2] [CIR] Upstream three way compare op

---
 .../include/clang/CIR/Dialect/IR/CIRAttrs.td  |  83 +++++
 clang/include/clang/CIR/Dialect/IR/CIROps.td  |  68 ++++
 clang/lib/CIR/CodeGen/CIRGenBuilder.h         |  35 ++
 clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp |  63 +++-
 clang/lib/CIR/Dialect/IR/CIRAttrs.cpp         |  60 ++++
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp       |   5 +
 .../Dialect/Transforms/LoweringPrepare.cpp    |  45 ++-
 clang/test/CIR/CodeGen/Inputs/std-compare.h   | 307 ++++++++++++++++++
 clang/test/CIR/CodeGen/three-way-cmp.cpp      |  99 ++++++
 clang/test/CIR/IR/invalid-cmp3way.cir         |  14 +
 10 files changed, 777 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/Inputs/std-compare.h
 create mode 100644 clang/test/CIR/CodeGen/three-way-cmp.cpp
 create mode 100644 clang/test/CIR/IR/invalid-cmp3way.cir

diff --git a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td 
b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
index b1be1d5daf4e0..1136d91ee4b0a 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIRAttrs.td
@@ -573,6 +573,89 @@ def CIR_MethodAttr : CIR_Attr<"Method", "method", 
[TypedAttrInterface]> {
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// CmpThreeWayInfoAttr
+//===----------------------------------------------------------------------===//
+
+def CIR_CmpOrdering : CIR_I32EnumAttr<
+  "CmpOrdering", "three-way comparison ordering kind", [
+    I32EnumAttrCase<"Strong", 0, "strong">,
+    I32EnumAttrCase<"Weak", 1, "weak">,
+    I32EnumAttrCase<"Partial", 2, "partial">
+]> {
+  let genSpecializedAttr = 0;
+}
+
+def CIR_CmpThreeWayInfoAttr : CIR_Attr<"CmpThreeWayInfo", "cmp3way_info"> {
+  let summary = "Holds information about a three-way comparison operation";
+  let description = [{
+    The `#cmpinfo` attribute contains information about a three-way
+    comparison operation `cir.cmp3way`.
+
+    The `ordering` parameter gives the ordering kind of the three-way 
comparison
+    operation: strong ordering, weak ordering, or partial ordering. Strong and
+    weak orderings are both total orderings (i.e. every two elements are 
comparable),
+    while partial orderings can have incomparable elements.
+
+    Given the two input operands of the three-way comparison operation `lhs` 
and
+    `rhs`, the `lt`, `eq`, `gt`, and `unordered` parameters gives the result
+    value that should be produced by the three-way comparison operation when 
the
+    ordering between `lhs` and `rhs` is `lhs < rhs`, `lhs == rhs`, `lhs > rhs`,
+    or neither, respectively.
+
+    Example:
+
+    ```mlir
+    !s32i = !cir.int<s, 32>
+
+    #cmpinfo_partial_ltn1eq0gt1unn127 = #cir.cmp3way_info<partial, lt = -1, eq 
= 0, gt = 1, unordered = -127>
+    #cmpinfo_strong_ltn1eq0gt1 = #cir.cmp3way_info<strong, lt = -1, eq = 0, gt 
= 1>
+
+    %0 = cir.const #cir.int<0> : !s32i
+    %1 = cir.const #cir.int<1> : !s32i
+    %2 = cir.cmp3way(%0 : !s32i, %1, #cmpinfo_strong_ltn1eq0gt1) : !s8i
+
+    %3 = cir.const #cir.fp<0.0> : !cir.float
+    %4 = cir.const #cir.fp<1.0> : !cir.float
+    %5 = cir.cmp3way(%3 : !cir.float, %4, #cmpinfo_partial_ltn1eq0gt1unn127) : 
!s8
+    ```
+  }];
+
+  let parameters = (ins
+    EnumParameter<CIR_CmpOrdering>:$ordering,
+    "int64_t":$lt, "int64_t":$eq, "int64_t":$gt,
+    OptionalParameter<"std::optional<int64_t>">:$unordered
+  );
+
+  let builders = [
+    AttrBuilder<(ins "CmpOrdering":$ordering, "int64_t":$lt, "int64_t":$eq,
+                     "int64_t":$gt), [{
+      return $_get($_ctxt, ordering, lt, eq, gt, std::nullopt);
+    }]>,
+    AttrBuilder<(ins "int64_t":$lt, "int64_t":$eq, "int64_t":$gt,
+                     "int64_t":$unordered), [{
+      return $_get($_ctxt, CmpOrdering::Partial, lt, eq, gt, unordered);
+    }]>,
+  ];
+
+  let extraClassDeclaration = [{
+    /// Get attribute alias name for this attribute.
+    std::string getAlias() const;
+  }];
+
+  let assemblyFormat = [{
+    `<`
+      $ordering `,`
+      `lt` `=` $lt `,`
+      `eq` `=` $eq `,`
+      `gt` `=` $gt
+      (`,` `unordered` `=` $unordered^)?
+    `>`
+  }];
+
+  let genVerifyDecl = 1;
+}
+
 
//===----------------------------------------------------------------------===//
 // GlobalViewAttr
 
//===----------------------------------------------------------------------===//
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 2c109eaeb392e..7532f200c98b2 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -1280,6 +1280,74 @@ def CIR_CleanupScopeOp : CIR_Op<"cleanup.scope", [
   let hasLLVMLowering = false;
 }
 
+//===----------------------------------------------------------------------===//
+// CmpThreeWayOp
+//===----------------------------------------------------------------------===//
+
+def CIR_CmpThreeWayOp : CIR_Op<"cmp3way", [Pure, SameTypeOperands]> {
+  let summary = "Compare two values with C++ three-way comparison semantics";
+  let description = [{
+    The `cir.cmp3way` operation models the builtin `<=>` operator in C++20.
+    It takes two operands with the same type and produces a result indicating
+    the ordering between the two input operands.
+
+    The result of the operation is a signed integer that indicates the ordering
+    between the two input operands.
+
+    There are three kinds of ordering: strong, weak and partial ordering.
+    Comparing different types of values yields different kinds of orderings.
+    The `info` parameter gives the ordering kind and other necessary 
information
+    about the comparison.
+
+    Example:
+
+    ```mlir
+    !s32i = !cir.int<s, 32>
+
+    #cmpinfo_partial_ltn1eq0gt1unn127 =
+      #cir.cmp3way_info<partial, lt = -1, eq = 0, gt = 1, unordered = -127>
+    #cmpinfo_strong_ltn1eq0gt1 =
+      #cir.cmp3way_info<strong, lt = -1, eq = 0, gt = 1>
+
+    %0 = cir.const #cir.int<0> : !s32i
+    %1 = cir.const #cir.int<1> : !s32i
+    %2 = cir.cmp3way #cmpinfo_strong_ltn1eq0gt1 %0, %1 : !s32i -> !s8i
+
+    %3 = cir.const #cir.fp<0.0> : !cir.float
+    %4 = cir.const #cir.fp<1.0> : !cir.float
+    %5 = cir.cmp3way #cmpinfo_partial_ltn1eq0gt1unn127 %3, %4 : !cir.float -> 
!s8i
+    ```
+  }];
+
+  let arguments = (ins
+    CIR_AnyType:$lhs,
+    CIR_AnyType:$rhs,
+    CIR_CmpThreeWayInfoAttr:$info
+  );
+
+  let results = (outs CIR_AnySIntType:$result);
+
+  let assemblyFormat = [{
+    qualified($info) $lhs `,` $rhs `:` qualified(type($lhs))
+    `->` qualified(type($result)) attr-dict
+  }];
+
+  let extraClassDeclaration = [{
+    /// Determine whether this three-way comparison produces a partial ordering
+    bool isPartialOrdering() {
+      cir::CmpOrdering o = getInfo().getOrdering();
+      return o == cir::CmpOrdering::Partial;
+    }
+
+    /// Determine whether this three-way comparison compares integral operands.
+    bool isIntegralComparison() {
+      return mlir::isa<cir::IntType>(getLhs().getType());
+    }
+  }];
+
+  let hasLLVMLowering = false;
+}
+
 
//===----------------------------------------------------------------------===//
 // SwitchOp
 
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h 
b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 7cd1bdcf491be..a073178e78cd5 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -13,6 +13,7 @@
 #include "CIRGenRecordLayout.h"
 #include "CIRGenTypeCache.h"
 #include "mlir/IR/Attributes.h"
+#include "mlir/IR/Builders.h"
 #include "mlir/IR/BuiltinAttributes.h"
 #include "mlir/Support/LLVM.h"
 #include "clang/CIR/Dialect/IR/CIRDataLayout.h"
@@ -716,6 +717,40 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
     return cir::StackRestoreOp::create(*this, loc, v);
   }
 
+  cir::CmpThreeWayOp createThreeWayCmpTotalOrdering(
+      mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
+      const llvm::APSInt &ltRes, const llvm::APSInt &eqRes,
+      const llvm::APSInt &gtRes, cir::CmpOrdering ordering) {
+    assert(ltRes.getBitWidth() == eqRes.getBitWidth() &&
+           ltRes.getBitWidth() == gtRes.getBitWidth() &&
+           "the three comparison results must have the same bit width");
+    assert((ordering == cir::CmpOrdering::Strong ||
+            ordering == cir::CmpOrdering::Weak) &&
+           "total ordering must be strong or weak");
+    cir::IntType cmpResultTy = getSIntNTy(ltRes.getBitWidth());
+    auto infoAttr = cir::CmpThreeWayInfoAttr::get(
+        getContext(), ordering, ltRes.getSExtValue(), eqRes.getSExtValue(),
+        gtRes.getSExtValue());
+    return cir::CmpThreeWayOp::create(*this, loc, cmpResultTy, lhs, rhs,
+                                      infoAttr);
+  }
+
+  cir::CmpThreeWayOp createThreeWayCmpPartialOrdering(
+      mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
+      const llvm::APSInt &ltRes, const llvm::APSInt &eqRes,
+      const llvm::APSInt &gtRes, const llvm::APSInt &unorderedRes) {
+    assert(ltRes.getBitWidth() == eqRes.getBitWidth() &&
+           ltRes.getBitWidth() == gtRes.getBitWidth() &&
+           ltRes.getBitWidth() == unorderedRes.getBitWidth() &&
+           "the four comparison results must have the same bit width");
+    cir::IntType cmpResultTy = getSIntNTy(ltRes.getBitWidth());
+    auto infoAttr = cir::CmpThreeWayInfoAttr::get(
+        getContext(), ltRes.getSExtValue(), eqRes.getSExtValue(),
+        gtRes.getSExtValue(), unorderedRes.getSExtValue());
+    return cir::CmpThreeWayOp::create(*this, loc, cmpResultTy, lhs, rhs,
+                                      infoAttr);
+  }
+
   mlir::Value createSetBitfield(mlir::Location loc, mlir::Type resultType,
                                 Address dstAddr, mlir::Type storageType,
                                 mlir::Value src, const CIRGenBitFieldInfo 
&info,
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
index 9f390fec97613..69dc5f53abc85 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprAggregate.cpp
@@ -19,6 +19,7 @@
 #include "clang/AST/Expr.h"
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtVisitor.h"
+#include "llvm/IR/Value.h"
 #include <cstdint>
 
 using namespace clang;
@@ -326,8 +327,68 @@ class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
     Visit(e->getRHS());
   }
   void VisitBinCmp(const BinaryOperator *e) {
-    cgf.cgm.errorNYI(e->getSourceRange(), "AggExprEmitter: VisitBinCmp");
+    assert(cgf.getContext().hasSameType(e->getLHS()->getType(),
+                                        e->getRHS()->getType()));
+    const ComparisonCategoryInfo &cmpInfo =
+        cgf.getContext().CompCategories.getInfoForType(e->getType());
+    assert(cmpInfo.Record->isTriviallyCopyable() &&
+           "cannot copy non-trivially copyable aggregate");
+
+    QualType argTy = e->getLHS()->getType();
+
+    if (!argTy->isIntegralOrEnumerationType() && !argTy->isRealFloatingType() 
&&
+        !argTy->isNullPtrType() && !argTy->isPointerType() &&
+        !argTy->isMemberPointerType() && !argTy->isAnyComplexType())
+      cgf.cgm.errorNYI(e->getBeginLoc(), "aggregate three-way comparison");
+
+    mlir::Location loc = cgf.getLoc(e->getSourceRange());
+    CIRGenBuilderTy builder = cgf.getBuilder();
+
+    if (e->getType()->isAnyComplexType())
+      cgf.cgm.errorNYI(e->getBeginLoc(), "VisitBinCmp: complex type");
+
+    if (e->getType()->isAggregateType())
+      cgf.cgm.errorNYI(e->getBeginLoc(), "VisitBinCmp: aggregate type");
+
+    mlir::Value lhs = cgf.emitAnyExpr(e->getLHS()).getValue();
+    mlir::Value rhs = cgf.emitAnyExpr(e->getRHS()).getValue();
+
+    mlir::Value resultScalar;
+    if (argTy->isNullPtrType()) {
+      resultScalar =
+          builder.getConstInt(loc, cmpInfo.getEqualOrEquiv()->getIntValue());
+    } else {
+      llvm::APSInt ltRes = cmpInfo.getLess()->getIntValue();
+      llvm::APSInt eqRes = cmpInfo.getEqualOrEquiv()->getIntValue();
+      llvm::APSInt gtRes = cmpInfo.getGreater()->getIntValue();
+      if (!cmpInfo.isPartial()) {
+        cir::CmpOrdering ordering = cmpInfo.isStrong()
+                                        ? cir::CmpOrdering::Strong
+                                        : cir::CmpOrdering::Weak;
+        resultScalar = builder.createThreeWayCmpTotalOrdering(
+            loc, lhs, rhs, ltRes, eqRes, gtRes, ordering);
+      } else {
+        // Partial ordering.
+        llvm::APSInt unorderedRes = cmpInfo.getUnordered()->getIntValue();
+        resultScalar = builder.createThreeWayCmpPartialOrdering(
+            loc, lhs, rhs, ltRes, eqRes, gtRes, unorderedRes);
+      }
+    }
+
+    // Create the return value in the destination slot.
+    ensureDest(loc, e->getType());
+    LValue destLVal = cgf.makeAddrLValue(dest.getAddress(), e->getType());
+
+    // Emit the address of the first (and only) field in the comparison 
category
+    // type, and initialize it from the constant integer value produced above.
+    const FieldDecl *resultField = *cmpInfo.Record->field_begin();
+    LValue fieldLVal = cgf.emitLValueForFieldInitialization(
+        destLVal, resultField, resultField->getName());
+    cgf.emitStoreThroughLValue(RValue::get(resultScalar), fieldLVal);
+
+    // All done! The result is in the dest slot.
   }
+
   void VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *e) {
     cgf.cgm.errorNYI(e->getSourceRange(),
                      "AggExprEmitter: VisitCXXRewrittenBinaryOperator");
diff --git a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp 
b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp
index 4cd2073bf49aa..d608038dafc5f 100644
--- a/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRAttrs.cpp
@@ -349,6 +349,66 @@ LogicalResult 
FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
   return success();
 }
 
+//===----------------------------------------------------------------------===//
+// CmpThreeWayInfoAttr definitions
+//===----------------------------------------------------------------------===//
+
+std::string CmpThreeWayInfoAttr::getAlias() const {
+  std::string alias = "cmpinfo";
+
+  switch (getOrdering()) {
+  case CmpOrdering::Strong:
+    alias.append("_strong_");
+    break;
+  case CmpOrdering::Weak:
+    alias.append("_weak_");
+    break;
+  case CmpOrdering::Partial:
+    alias.append("_partial_");
+    break;
+  }
+
+  auto appendInt = [&](int64_t value) {
+    if (value < 0) {
+      alias.push_back('n');
+      value = -value;
+    }
+    alias.append(std::to_string(value));
+  };
+
+  alias.append("lt");
+  appendInt(getLt());
+  alias.append("eq");
+  appendInt(getEq());
+  alias.append("gt");
+  appendInt(getGt());
+
+  if (std::optional<int> unordered = getUnordered()) {
+    alias.append("un");
+    appendInt(unordered.value());
+  }
+
+  return alias;
+}
+
+LogicalResult
+CmpThreeWayInfoAttr::verify(function_ref<InFlightDiagnostic()> emitError,
+                            CmpOrdering ordering, int64_t lt, int64_t eq,
+                            int64_t gt, std::optional<int64_t> unordered) {
+  // The presence of unordered must match the value of ordering.
+  if ((ordering == CmpOrdering::Strong || ordering == CmpOrdering::Weak) &&
+      unordered) {
+    emitError() << "strong and weak ordering do not include unordered";
+    return failure();
+  }
+  if (ordering == CmpOrdering::Partial && !unordered) {
+    emitError() << "partial ordering requires unordered value";
+    return failure();
+  }
+
+  return success();
+}
+
 
//===----------------------------------------------------------------------===//
 // ConstComplexAttr definitions
 
//===----------------------------------------------------------------------===//
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 8d2990af5de8c..b5e4a8cad59c5 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -80,6 +80,11 @@ struct CIROpAsmDialectInterface : public 
OpAsmDialectInterface {
       os << dynCastInfoAttr.getAlias();
       return AliasResult::FinalAlias;
     }
+    if (auto cmpThreeWayInfoAttr =
+            mlir::dyn_cast<cir::CmpThreeWayInfoAttr>(attr)) {
+      os << cmpThreeWayInfoAttr.getAlias();
+      return AliasResult::FinalAlias;
+    }
     return AliasResult::NoAlias;
   }
 };
diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp 
b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index 232d320d71f37..162d261f7065c 100644
--- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -75,6 +75,7 @@ struct LoweringPreparePass
   void lowerComplexMulOp(cir::ComplexMulOp op);
   void lowerUnaryOp(cir::UnaryOp op);
   void lowerGlobalOp(cir::GlobalOp op);
+  void lowerThreeWayCmpOp(cir::CmpThreeWayOp op);
   void lowerArrayDtor(cir::ArrayDtor op);
   void lowerArrayCtor(cir::ArrayCtor op);
   void lowerTrivialCopyCall(cir::CallOp op);
@@ -1262,6 +1263,46 @@ void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
   assert(!cir::MissingFeatures::opGlobalAnnotations());
 }
 
+void LoweringPreparePass::lowerThreeWayCmpOp(CmpThreeWayOp op) {
+  CIRBaseBuilderTy builder(getContext());
+  builder.setInsertionPointAfter(op);
+
+  mlir::Location loc = op->getLoc();
+  cir::CmpThreeWayInfoAttr cmpInfo = op.getInfo();
+
+  mlir::Value ltRes =
+      builder.getConstantInt(loc, op.getType(), cmpInfo.getLt());
+  mlir::Value eqRes =
+      builder.getConstantInt(loc, op.getType(), cmpInfo.getEq());
+  mlir::Value gtRes =
+      builder.getConstantInt(loc, op.getType(), cmpInfo.getGt());
+
+  mlir::Value lt =
+      builder.createCompare(loc, CmpOpKind::lt, op.getLhs(), op.getRhs());
+  mlir::Value eq =
+      builder.createCompare(loc, CmpOpKind::eq, op.getLhs(), op.getRhs());
+
+  mlir::Value transformedResult;
+  if (cmpInfo.getOrdering() != CmpOrdering::Partial) {
+    // Total ordering
+    mlir::Value selectOnLt = builder.createSelect(loc, lt, ltRes, gtRes);
+    transformedResult = builder.createSelect(loc, eq, eqRes, selectOnLt);
+  } else {
+    // Partial ordering
+    cir::ConstantOp unorderedRes = builder.getConstantInt(
+        loc, op.getType(), cmpInfo.getUnordered().value());
+
+    mlir::Value selectOnEq = builder.createSelect(loc, eq, eqRes, 
unorderedRes);
+    mlir::Value gt =
+        builder.createCompare(loc, CmpOpKind::gt, op.getLhs(), op.getRhs());
+    mlir::Value selectOnGt = builder.createSelect(loc, gt, gtRes, selectOnEq);
+    transformedResult = builder.createSelect(loc, lt, ltRes, selectOnGt);
+  }
+
+  op.replaceAllUsesWith(transformedResult);
+  op.erase();
+}
+
 template <typename AttributeTy>
 static llvm::SmallVector<mlir::Attribute>
 prepareCtorDtorAttrList(mlir::MLIRContext *context,
@@ -1559,6 +1600,8 @@ void LoweringPreparePass::runOnOp(mlir::Operation *op) {
       globalCtorList.emplace_back(fnOp.getName(), globalCtor.value());
     else if (auto globalDtor = fnOp.getGlobalDtorPriority())
       globalDtorList.emplace_back(fnOp.getName(), globalDtor.value());
+  } else if (auto threeWayCmp = dyn_cast<cir::CmpThreeWayOp>(op)) {
+    lowerThreeWayCmpOp(threeWayCmp);
   }
 }
 
@@ -1573,7 +1616,7 @@ void LoweringPreparePass::runOnOperation() {
     if (mlir::isa<cir::ArrayCtor, cir::ArrayDtor, cir::CastOp,
                   cir::ComplexMulOp, cir::ComplexDivOp, cir::DynamicCastOp,
                   cir::FuncOp, cir::CallOp, cir::GetGlobalOp, cir::GlobalOp,
-                  cir::StoreOp, cir::UnaryOp>(op))
+                  cir::StoreOp, cir::UnaryOp, cir::CmpThreeWayOp>(op))
       opsToTransform.push_back(op);
   });
 
diff --git a/clang/test/CIR/CodeGen/Inputs/std-compare.h 
b/clang/test/CIR/CodeGen/Inputs/std-compare.h
new file mode 100644
index 0000000000000..eaf7951edf79c
--- /dev/null
+++ b/clang/test/CIR/CodeGen/Inputs/std-compare.h
@@ -0,0 +1,307 @@
+#ifndef STD_COMPARE_H
+#define STD_COMPARE_H
+
+namespace std {
+inline namespace __1 {
+
+// exposition only
+enum class _EqResult : unsigned char {
+  __equal = 0,
+  __equiv = __equal,
+};
+
+enum class _OrdResult : signed char {
+  __less = -1,
+  __greater = 1
+};
+
+enum class _NCmpResult : signed char {
+  __unordered = -127
+};
+
+struct _CmpUnspecifiedType;
+using _CmpUnspecifiedParam = void (_CmpUnspecifiedType::*)();
+
+class partial_ordering {
+  using _ValueT = signed char;
+  explicit constexpr partial_ordering(_EqResult __v) noexcept
+      : __value_(_ValueT(__v)) {}
+  explicit constexpr partial_ordering(_OrdResult __v) noexcept
+      : __value_(_ValueT(__v)) {}
+  explicit constexpr partial_ordering(_NCmpResult __v) noexcept
+      : __value_(_ValueT(__v)) {}
+
+  constexpr bool __is_ordered() const noexcept {
+    return __value_ != _ValueT(_NCmpResult::__unordered);
+  }
+
+public:
+  // valid values
+  static const partial_ordering less;
+  static const partial_ordering equivalent;
+  static const partial_ordering greater;
+  static const partial_ordering unordered;
+
+  // comparisons
+  friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator!=(partial_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator==(_CmpUnspecifiedParam, partial_ordering __v) 
noexcept;
+  friend constexpr bool operator!=(_CmpUnspecifiedParam, partial_ordering __v) 
noexcept;
+  friend constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) 
noexcept;
+  friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) 
noexcept;
+  friend constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) 
noexcept;
+  friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) 
noexcept;
+
+  friend constexpr partial_ordering operator<=>(partial_ordering __v, 
_CmpUnspecifiedParam) noexcept;
+  friend constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, 
partial_ordering __v) noexcept;
+
+  // test helper
+  constexpr bool test_eq(partial_ordering const &other) const noexcept {
+    return __value_ == other.__value_;
+  }
+
+private:
+  _ValueT __value_;
+};
+
+inline constexpr partial_ordering partial_ordering::less(_OrdResult::__less);
+inline constexpr partial_ordering 
partial_ordering::equivalent(_EqResult::__equiv);
+inline constexpr partial_ordering 
partial_ordering::greater(_OrdResult::__greater);
+inline constexpr partial_ordering partial_ordering::unordered(_NCmpResult 
::__unordered);
+constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept 
{
+  return __v.__is_ordered() && __v.__value_ == 0;
+}
+constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__is_ordered() && __v.__value_ < 0;
+}
+constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept 
{
+  return __v.__is_ordered() && __v.__value_ <= 0;
+}
+constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__is_ordered() && __v.__value_ > 0;
+}
+constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept 
{
+  return __v.__is_ordered() && __v.__value_ >= 0;
+}
+constexpr bool operator==(_CmpUnspecifiedParam, partial_ordering __v) noexcept 
{
+  return __v.__is_ordered() && 0 == __v.__value_;
+}
+constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
+  return __v.__is_ordered() && 0 < __v.__value_;
+}
+constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept 
{
+  return __v.__is_ordered() && 0 <= __v.__value_;
+}
+constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
+  return __v.__is_ordered() && 0 > __v.__value_;
+}
+constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept 
{
+  return __v.__is_ordered() && 0 >= __v.__value_;
+}
+constexpr bool operator!=(partial_ordering __v, _CmpUnspecifiedParam) noexcept 
{
+  return !__v.__is_ordered() || __v.__value_ != 0;
+}
+constexpr bool operator!=(_CmpUnspecifiedParam, partial_ordering __v) noexcept 
{
+  return !__v.__is_ordered() || __v.__value_ != 0;
+}
+
+constexpr partial_ordering operator<=>(partial_ordering __v, 
_CmpUnspecifiedParam) noexcept {
+  return __v;
+}
+constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, partial_ordering 
__v) noexcept {
+  return __v < 0 ? partial_ordering::greater : (__v > 0 ? 
partial_ordering::less : __v);
+}
+
+class weak_ordering {
+  using _ValueT = signed char;
+  explicit constexpr weak_ordering(_EqResult __v) noexcept : 
__value_(_ValueT(__v)) {}
+  explicit constexpr weak_ordering(_OrdResult __v) noexcept : 
__value_(_ValueT(__v)) {}
+
+public:
+  static const weak_ordering less;
+  static const weak_ordering equivalent;
+  static const weak_ordering greater;
+
+  // conversions
+  constexpr operator partial_ordering() const noexcept {
+    return __value_ == 0 ? partial_ordering::equivalent
+                         : (__value_ < 0 ? partial_ordering::less : 
partial_ordering::greater);
+  }
+
+  // comparisons
+  friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator!=(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator<(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator>(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator==(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept;
+  friend constexpr bool operator!=(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept;
+  friend constexpr bool operator<(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept;
+  friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept;
+  friend constexpr bool operator>(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept;
+  friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept;
+
+  friend constexpr weak_ordering operator<=>(weak_ordering __v, 
_CmpUnspecifiedParam) noexcept;
+  friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, 
weak_ordering __v) noexcept;
+
+  // test helper
+  constexpr bool test_eq(weak_ordering const &other) const noexcept {
+    return __value_ == other.__value_;
+  }
+
+private:
+  _ValueT __value_;
+};
+
+inline constexpr weak_ordering weak_ordering::less(_OrdResult::__less);
+inline constexpr weak_ordering weak_ordering::equivalent(_EqResult::__equiv);
+inline constexpr weak_ordering weak_ordering::greater(_OrdResult::__greater);
+constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ == 0;
+}
+constexpr bool operator!=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ != 0;
+}
+constexpr bool operator<(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ < 0;
+}
+constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ <= 0;
+}
+constexpr bool operator>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ > 0;
+}
+constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ >= 0;
+}
+constexpr bool operator==(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
+  return 0 == __v.__value_;
+}
+constexpr bool operator!=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
+  return 0 != __v.__value_;
+}
+constexpr bool operator<(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
+  return 0 < __v.__value_;
+}
+constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
+  return 0 <= __v.__value_;
+}
+constexpr bool operator>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
+  return 0 > __v.__value_;
+}
+constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
+  return 0 >= __v.__value_;
+}
+
+constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) 
noexcept {
+  return __v;
+}
+constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) 
noexcept {
+  return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : 
__v);
+}
+
+class strong_ordering {
+  using _ValueT = signed char;
+  explicit constexpr strong_ordering(_EqResult __v) noexcept : 
__value_(static_cast<signed char>(__v)) {}
+  explicit constexpr strong_ordering(_OrdResult __v) noexcept : 
__value_(static_cast<signed char>(__v)) {}
+
+public:
+  static const strong_ordering less;
+  static const strong_ordering equal;
+  static const strong_ordering equivalent;
+  static const strong_ordering greater;
+
+  // conversions
+  constexpr operator partial_ordering() const noexcept {
+    return __value_ == 0 ? partial_ordering::equivalent
+                         : (__value_ < 0 ? partial_ordering::less : 
partial_ordering::greater);
+  }
+  constexpr operator weak_ordering() const noexcept {
+    return __value_ == 0 ? weak_ordering::equivalent
+                         : (__value_ < 0 ? weak_ordering::less : 
weak_ordering::greater);
+  }
+
+  // comparisons
+  friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator!=(strong_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator<(strong_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator>(strong_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) 
noexcept;
+  friend constexpr bool operator==(_CmpUnspecifiedParam, strong_ordering __v) 
noexcept;
+  friend constexpr bool operator!=(_CmpUnspecifiedParam, strong_ordering __v) 
noexcept;
+  friend constexpr bool operator<(_CmpUnspecifiedParam, strong_ordering __v) 
noexcept;
+  friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) 
noexcept;
+  friend constexpr bool operator>(_CmpUnspecifiedParam, strong_ordering __v) 
noexcept;
+  friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) 
noexcept;
+
+  friend constexpr strong_ordering operator<=>(strong_ordering __v, 
_CmpUnspecifiedParam) noexcept;
+  friend constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, 
strong_ordering __v) noexcept;
+
+  // test helper
+  constexpr bool test_eq(strong_ordering const &other) const noexcept {
+    return __value_ == other.__value_;
+  }
+
+private:
+  _ValueT __value_;
+};
+
+inline constexpr strong_ordering strong_ordering::less(_OrdResult::__less);
+inline constexpr strong_ordering strong_ordering::equal(_EqResult::__equal);
+inline constexpr strong_ordering 
strong_ordering::equivalent(_EqResult::__equiv);
+inline constexpr strong_ordering 
strong_ordering::greater(_OrdResult::__greater);
+
+constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ == 0;
+}
+constexpr bool operator!=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ != 0;
+}
+constexpr bool operator<(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ < 0;
+}
+constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ <= 0;
+}
+constexpr bool operator>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ > 0;
+}
+constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
+  return __v.__value_ >= 0;
+}
+constexpr bool operator==(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
+  return 0 == __v.__value_;
+}
+constexpr bool operator!=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
+  return 0 != __v.__value_;
+}
+constexpr bool operator<(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
+  return 0 < __v.__value_;
+}
+constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
+  return 0 <= __v.__value_;
+}
+constexpr bool operator>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
+  return 0 > __v.__value_;
+}
+constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
+  return 0 >= __v.__value_;
+}
+
+constexpr strong_ordering operator<=>(strong_ordering __v, 
_CmpUnspecifiedParam) noexcept {
+  return __v;
+}
+constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, strong_ordering 
__v) noexcept {
+  return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less 
: __v);
+}
+
+} // namespace __1
+} // end namespace std
+
+#endif // STD_COMPARE_H
diff --git a/clang/test/CIR/CodeGen/three-way-cmp.cpp 
b/clang/test/CIR/CodeGen/three-way-cmp.cpp
new file mode 100644
index 0000000000000..f10dcb1715bbf
--- /dev/null
+++ b/clang/test/CIR/CodeGen/three-way-cmp.cpp
@@ -0,0 +1,99 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir 
-emit-cir -mmlir --mlir-print-ir-before=cir-lowering-prepare %s -o %t.cir 2> 
%t-before.cir
+// RUN: FileCheck %s --input-file=%t-before.cir --check-prefix=BEFORE
+// RUN: FileCheck %s --input-file=%t.cir --check-prefix=AFTER
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir 
-emit-cir -mmlir --mlir-print-ir-after=cir-lowering-prepare %s -o %t.cir 2>&1 | 
FileCheck %s -check-prefix=AFTER
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir 
-emit-llvm %s -o %t.ll 2>&1
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -emit-llvm %s 
-o %t-og.ll 2>&1
+// RUN: FileCheck --input-file=%t-og.ll %s -check-prefix=OGCG
+
+#include "./Inputs/std-compare.h"
+
+// BEFORE: #cmpinfo_partial_ltn1eq0gt1unn127 = #cir.cmp3way_info<partial, lt = 
-1, eq = 0, gt = 1, unordered = -127>
+// BEFORE: #cmpinfo_strong_ltn1eq0gt1 = #cir.cmp3way_info<strong, lt = -1, eq 
= 0, gt = 1>
+// BEFORE: !rec_std3A3A__13A3Apartial_ordering = !cir.record<class 
"std::__1::partial_ordering" {!s8i}>
+// BEFORE: !rec_std3A3A__13A3Astrong_ordering = !cir.record<class 
"std::__1::strong_ordering" {!s8i}>
+
+auto three_way_strong(int x, int y) {
+  return x <=> y;
+}
+
+// BEFORE: cir.func {{.*}} @_Z16three_way_strongii
+// BEFORE:   %{{.+}} = cir.cmp3way #cmpinfo_strong_ltn1eq0gt1 %{{.+}}, %{{.+}} 
: !s32i -> !s8i
+// BEFORE: }
+
+//      AFTER:   cir.func {{.*}} @_Z16three_way_strongii{{.*}}
+//      AFTER:   %[[LHS:.*]] = cir.load align(4) %{{.+}} : !cir.ptr<!s32i>, 
!s32i{{.*}}
+// AFTER-NEXT:   %[[RHS:.*]] = cir.load align(4) %{{.+}} : !cir.ptr<!s32i>, 
!s32i{{.*}}
+// AFTER-NEXT:   %[[LT:.*]] = cir.const #cir.int<-1> : !s8i{{.*}}
+// AFTER-NEXT:   %[[EQ:.*]] = cir.const #cir.int<0> : !s8i{{.*}}
+// AFTER-NEXT:   %[[GT:.*]] = cir.const #cir.int<1> : !s8i{{.*}}
+// AFTER-NEXT:   %[[CMP_LT:.*]] = cir.cmp lt %[[LHS]], %[[RHS]] : !s32i{{.*}}
+// AFTER-NEXT:   %[[CMP_EQ:.*]] = cir.cmp eq %[[LHS]], %[[RHS]] : !s32i{{.*}}
+// AFTER-NEXT:   %[[SELECT_1:.*]] = cir.select if %[[CMP_LT]] then %[[LT]] 
else %[[GT]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %[[SELECT_2:.*]] = cir.select if %[[CMP_EQ]] then %[[EQ]] 
else %[[SELECT_1]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %{{.+}} = cir.get_member %{{.+}}[0] {{.*}} "__value_"{{.*}}
+// AFTER-NEXT:   cir.store align(1) %[[SELECT_2]], %{{.+}} : !s8i, 
!cir.ptr<!s8i>{{.*}}
+// AFTER-NEXT:   %{{.+}} = cir.load %{{.+}} : 
!cir.ptr<!rec_std3A3A__13A3Astrong_ordering>, 
!rec_std3A3A__13A3Astrong_ordering{{.*}}
+// AFTER-NEXT:   cir.return %{{.+}} : !rec_std3A3A__13A3Astrong_ordering{{.*}}
+
+// LLVM:  %[[LHS:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM-NEXT:  %[[RHS:.*]] = load i32, ptr %{{.*}}, align 4
+// LLVM-NEXT:  %[[CMP_LT:.*]] = icmp slt i32 %[[LHS]], %[[RHS]]
+// LLVM-NEXT:  %[[CMP_EQ:.*]] = icmp eq i32 %[[LHS]], %[[RHS]]
+// LLVM-NEXT:  %[[SEL_LT_GT:.*]] = select i1 %[[CMP_LT]], i8 -1, i8 1
+// LLVM-NEXT:  %[[RES:.*]] = select i1 %[[CMP_EQ]], i8 0, i8 %[[SEL_LT_GT]]
+
+// OGCG:  %[[LHS:.*]] = load i32, ptr %{{.*}}, align 4
+// OGCG-NEXT:  %[[RHS:.*]] = load i32, ptr %{{.*}}, align 4
+// OGCG-NEXT:  %[[CMP_LT:.*]] = icmp slt i32 %[[LHS]], %[[RHS]]
+// OGCG-NEXT:  %[[SEL_EQ_LT:.*]] = select i1 %[[CMP_LT]], i8 -1, i8 1
+// OGCG-NEXT:  %[[CMP_EQ:.*]] = icmp eq i32 %[[LHS]], %[[RHS]]
+// OGCG-NEXT:  %[[RES:.*]] = select i1 %[[CMP_EQ]], i8 0, i8 %[[SEL_EQ_LT]]
+
+auto three_way_partial(float x, float y) {
+  return x <=> y;
+}
+
+// BEFORE: cir.func {{.*}} @_Z17three_way_partialff
+// BEFORE:   %{{.+}} = cir.cmp3way #cmpinfo_partial_ltn1eq0gt1unn127 %{{.+}}, 
%{{.+}} : !cir.float -> !s8i
+// BEFORE: }
+
+//      AFTER:   cir.func {{.*}} @_Z17three_way_partialff{{.*}}
+//      AFTER:   %[[LHS:.*]] = cir.load align(4) %{{.+}} : 
!cir.ptr<!cir.float>, !cir.float{{.*}}
+// AFTER-NEXT:   %[[RHS:.*]] = cir.load align(4) %{{.+}} : 
!cir.ptr<!cir.float>, !cir.float{{.*}}
+// AFTER-NEXT:   %[[LT:.*]] = cir.const #cir.int<-1> : !s8i{{.*}}
+// AFTER-NEXT:   %[[EQ:.*]] = cir.const #cir.int<0> : !s8i{{.*}}
+// AFTER-NEXT:   %[[GT:.*]] = cir.const #cir.int<1> : !s8i{{.*}}
+// AFTER-NEXT:   %[[CMP_LT:.*]] = cir.cmp lt %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
+// AFTER-NEXT:   %[[CMP_EQ:.*]] = cir.cmp eq %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
+// AFTER-NEXT:   %[[UNORDERED:.*]] = cir.const #cir.int<-127> : !s8i{{.*}}
+// AFTER-NEXT:   %[[SELECT_1:.*]] = cir.select if %[[CMP_EQ]] then %[[EQ]] 
else %[[UNORDERED]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %[[CMP_GT:.*]] = cir.cmp gt %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
+// AFTER-NEXT:   %[[SELECT_2:.*]] = cir.select if %[[CMP_GT]] then %[[GT]] 
else %[[SELECT_1]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %[[SELECT_3:.*]] = cir.select if %[[CMP_LT]] then %[[LT]] 
else %[[SELECT_2]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %{{.+}} = cir.get_member %{{.+}}[0] {{.*}} "__value_"{{.*}}
+// AFTER-NEXT:   cir.store align(1) %[[SELECT_3]], %{{.+}} : !s8i, 
!cir.ptr<!s8i>{{.*}}
+// AFTER-NEXT:   %{{.+}} = cir.load %{{.+}} : 
!cir.ptr<!rec_std3A3A__13A3Apartial_ordering>, 
!rec_std3A3A__13A3Apartial_ordering{{.*}}
+// AFTER-NEXT:   cir.return %{{.+}} : !rec_std3A3A__13A3Apartial_ordering{{.*}}
+
+// LLVM:  %[[LHS:.*]] = load float, ptr %{{.*}}, align 4
+// LLVM:  %[[RHS:.*]] = load float, ptr %{{.*}}, align 4
+// LLVM:  %[[CMP_LT:.*]] = fcmp olt float %[[LHS]], %[[RHS]]
+// LLVM:  %[[CMP_EQ:.*]] = fcmp oeq float %[[LHS]], %[[RHS]]
+// LLVM:  %[[SEL_EQ_UN:.*]] = select i1 %[[CMP_EQ]], i8 0, i8 -127
+// LLVM:  %[[CMP_GT:.*]] = fcmp ogt float %[[LHS]], %[[RHS]]
+// LLVM:  %[[SEL_GT_EQUN:.*]] = select i1 %[[CMP_GT]], i8 1, i8 %[[SEL_EQ_UN]]
+// LLVM:  %[[RES:.*]] = select i1 %[[CMP_LT]], i8 -1, i8 %[[SEL_GT_EQUN]]
+
+// OGCG:  %[[LHS:.*]] = load float, ptr %{{.*}}, align 4
+// OGCG:  %[[RHS:.*]] = load float, ptr %{{.*}}, align 4
+// OGCG:  %[[CMP_EQ:.*]] = fcmp oeq float %[[LHS]], %[[RHS]]
+// OGCG:  %[[SEL_EQ_UN:.*]] = select i1 %[[CMP_EQ]], i8 0, i8 -127
+// OGCG:  %[[CMP_GT:.*]] = fcmp ogt float %[[LHS]], %[[RHS]]
+// OGCG:  %[[SEL_GT_EQUN:.*]] = select i1 %[[CMP_GT]], i8 1, i8 %[[SEL_EQ_UN]]
+// OGCG:  %[[CMP_LT:.*]] = fcmp olt float %[[LHS]], %[[RHS]]
+// OGCG:  %[[RES:.*]] = select i1 %[[CMP_LT]], i8 -1, i8 %[[SEL_GT_EQUN]]
diff --git a/clang/test/CIR/IR/invalid-cmp3way.cir 
b/clang/test/CIR/IR/invalid-cmp3way.cir
new file mode 100644
index 0000000000000..77a8d8e90e00b
--- /dev/null
+++ b/clang/test/CIR/IR/invalid-cmp3way.cir
@@ -0,0 +1,14 @@
+// RUN: cir-opt %s -verify-diagnostics -split-input-file
+
+
+// expected-error@+1 {{strong and weak ordering do not include unordered}}
+#cmpinfo_strong_ltn1eq0gt1unn127 = #cir.cmp3way_info<strong, lt = -1, eq = 0, 
gt = 1, unordered = -127>
+// -----
+
+// expected-error@+1 {{strong and weak ordering do not include unordered}}
+#cmpinfo_weak_ltn1eq0gt1unn127 = #cir.cmp3way_info<weak, lt = -1, eq = 0, gt = 
1, unordered = -127>
+
+// -----
+
+// expected-error@+1 {{partial ordering requires unordered value}}
+#cmp3way_info_partial_ltn1eq0gt1 = #cir.cmp3way_info<partial, lt = -1, eq = 0, 
gt = 1>

From 98602e08fdb07b12663f00b4113159754fe24d61 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?=
 <[email protected]>
Date: Sat, 14 Mar 2026 15:05:36 +0100
Subject: [PATCH 2/2] match ogcg

---
 .../CIR/Dialect/Transforms/LoweringPrepare.cpp | 12 ++++++++----
 clang/test/CIR/CodeGen/three-way-cmp.cpp       | 18 +++++++-----------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp 
b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
index 162d261f7065c..8a624de635af2 100644
--- a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -1277,25 +1277,29 @@ void 
LoweringPreparePass::lowerThreeWayCmpOp(CmpThreeWayOp op) {
   mlir::Value gtRes =
       builder.getConstantInt(loc, op.getType(), cmpInfo.getGt());
 
-  mlir::Value lt =
-      builder.createCompare(loc, CmpOpKind::lt, op.getLhs(), op.getRhs());
-  mlir::Value eq =
-      builder.createCompare(loc, CmpOpKind::eq, op.getLhs(), op.getRhs());
 
   mlir::Value transformedResult;
   if (cmpInfo.getOrdering() != CmpOrdering::Partial) {
     // Total ordering
+    mlir::Value lt =
+        builder.createCompare(loc, CmpOpKind::lt, op.getLhs(), op.getRhs());
     mlir::Value selectOnLt = builder.createSelect(loc, lt, ltRes, gtRes);
+    mlir::Value eq =
+        builder.createCompare(loc, CmpOpKind::eq, op.getLhs(), op.getRhs());
     transformedResult = builder.createSelect(loc, eq, eqRes, selectOnLt);
   } else {
     // Partial ordering
     cir::ConstantOp unorderedRes = builder.getConstantInt(
         loc, op.getType(), cmpInfo.getUnordered().value());
 
+    mlir::Value eq =
+        builder.createCompare(loc, CmpOpKind::eq, op.getLhs(), op.getRhs());
     mlir::Value selectOnEq = builder.createSelect(loc, eq, eqRes, 
unorderedRes);
     mlir::Value gt =
         builder.createCompare(loc, CmpOpKind::gt, op.getLhs(), op.getRhs());
     mlir::Value selectOnGt = builder.createSelect(loc, gt, gtRes, selectOnEq);
+    mlir::Value lt =
+        builder.createCompare(loc, CmpOpKind::lt, op.getLhs(), op.getRhs());
     transformedResult = builder.createSelect(loc, lt, ltRes, selectOnGt);
   }
 
diff --git a/clang/test/CIR/CodeGen/three-way-cmp.cpp 
b/clang/test/CIR/CodeGen/three-way-cmp.cpp
index f10dcb1715bbf..e59483206a2eb 100644
--- a/clang/test/CIR/CodeGen/three-way-cmp.cpp
+++ b/clang/test/CIR/CodeGen/three-way-cmp.cpp
@@ -32,19 +32,17 @@ auto three_way_strong(int x, int y) {
 // AFTER-NEXT:   %[[EQ:.*]] = cir.const #cir.int<0> : !s8i{{.*}}
 // AFTER-NEXT:   %[[GT:.*]] = cir.const #cir.int<1> : !s8i{{.*}}
 // AFTER-NEXT:   %[[CMP_LT:.*]] = cir.cmp lt %[[LHS]], %[[RHS]] : !s32i{{.*}}
-// AFTER-NEXT:   %[[CMP_EQ:.*]] = cir.cmp eq %[[LHS]], %[[RHS]] : !s32i{{.*}}
 // AFTER-NEXT:   %[[SELECT_1:.*]] = cir.select if %[[CMP_LT]] then %[[LT]] 
else %[[GT]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %[[CMP_EQ:.*]] = cir.cmp eq %[[LHS]], %[[RHS]] : !s32i{{.*}}
 // AFTER-NEXT:   %[[SELECT_2:.*]] = cir.select if %[[CMP_EQ]] then %[[EQ]] 
else %[[SELECT_1]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
-// AFTER-NEXT:   %{{.+}} = cir.get_member %{{.+}}[0] {{.*}} "__value_"{{.*}}
-// AFTER-NEXT:   cir.store align(1) %[[SELECT_2]], %{{.+}} : !s8i, 
!cir.ptr<!s8i>{{.*}}
-// AFTER-NEXT:   %{{.+}} = cir.load %{{.+}} : 
!cir.ptr<!rec_std3A3A__13A3Astrong_ordering>, 
!rec_std3A3A__13A3Astrong_ordering{{.*}}
+// AFTER:   %{{.+}} = cir.load %{{.+}} : 
!cir.ptr<!rec_std3A3A__13A3Astrong_ordering>, 
!rec_std3A3A__13A3Astrong_ordering{{.*}}
 // AFTER-NEXT:   cir.return %{{.+}} : !rec_std3A3A__13A3Astrong_ordering{{.*}}
 
 // LLVM:  %[[LHS:.*]] = load i32, ptr %{{.*}}, align 4
 // LLVM-NEXT:  %[[RHS:.*]] = load i32, ptr %{{.*}}, align 4
 // LLVM-NEXT:  %[[CMP_LT:.*]] = icmp slt i32 %[[LHS]], %[[RHS]]
-// LLVM-NEXT:  %[[CMP_EQ:.*]] = icmp eq i32 %[[LHS]], %[[RHS]]
 // LLVM-NEXT:  %[[SEL_LT_GT:.*]] = select i1 %[[CMP_LT]], i8 -1, i8 1
+// LLVM-NEXT:  %[[CMP_EQ:.*]] = icmp eq i32 %[[LHS]], %[[RHS]]
 // LLVM-NEXT:  %[[RES:.*]] = select i1 %[[CMP_EQ]], i8 0, i8 %[[SEL_LT_GT]]
 
 // OGCG:  %[[LHS:.*]] = load i32, ptr %{{.*}}, align 4
@@ -68,25 +66,23 @@ auto three_way_partial(float x, float y) {
 // AFTER-NEXT:   %[[LT:.*]] = cir.const #cir.int<-1> : !s8i{{.*}}
 // AFTER-NEXT:   %[[EQ:.*]] = cir.const #cir.int<0> : !s8i{{.*}}
 // AFTER-NEXT:   %[[GT:.*]] = cir.const #cir.int<1> : !s8i{{.*}}
-// AFTER-NEXT:   %[[CMP_LT:.*]] = cir.cmp lt %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
-// AFTER-NEXT:   %[[CMP_EQ:.*]] = cir.cmp eq %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
 // AFTER-NEXT:   %[[UNORDERED:.*]] = cir.const #cir.int<-127> : !s8i{{.*}}
+// AFTER-NEXT:   %[[CMP_EQ:.*]] = cir.cmp eq %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
 // AFTER-NEXT:   %[[SELECT_1:.*]] = cir.select if %[[CMP_EQ]] then %[[EQ]] 
else %[[UNORDERED]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
 // AFTER-NEXT:   %[[CMP_GT:.*]] = cir.cmp gt %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
 // AFTER-NEXT:   %[[SELECT_2:.*]] = cir.select if %[[CMP_GT]] then %[[GT]] 
else %[[SELECT_1]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
+// AFTER-NEXT:   %[[CMP_LT:.*]] = cir.cmp lt %[[LHS]], %[[RHS]] : 
!cir.float{{.*}}
 // AFTER-NEXT:   %[[SELECT_3:.*]] = cir.select if %[[CMP_LT]] then %[[LT]] 
else %[[SELECT_2]] : (!cir.bool, !s8i, !s8i) -> !s8i{{.*}}
-// AFTER-NEXT:   %{{.+}} = cir.get_member %{{.+}}[0] {{.*}} "__value_"{{.*}}
-// AFTER-NEXT:   cir.store align(1) %[[SELECT_3]], %{{.+}} : !s8i, 
!cir.ptr<!s8i>{{.*}}
-// AFTER-NEXT:   %{{.+}} = cir.load %{{.+}} : 
!cir.ptr<!rec_std3A3A__13A3Apartial_ordering>, 
!rec_std3A3A__13A3Apartial_ordering{{.*}}
+// AFTER:   %{{.+}} = cir.load %{{.+}} : 
!cir.ptr<!rec_std3A3A__13A3Apartial_ordering>, 
!rec_std3A3A__13A3Apartial_ordering{{.*}}
 // AFTER-NEXT:   cir.return %{{.+}} : !rec_std3A3A__13A3Apartial_ordering{{.*}}
 
 // LLVM:  %[[LHS:.*]] = load float, ptr %{{.*}}, align 4
 // LLVM:  %[[RHS:.*]] = load float, ptr %{{.*}}, align 4
-// LLVM:  %[[CMP_LT:.*]] = fcmp olt float %[[LHS]], %[[RHS]]
 // LLVM:  %[[CMP_EQ:.*]] = fcmp oeq float %[[LHS]], %[[RHS]]
 // LLVM:  %[[SEL_EQ_UN:.*]] = select i1 %[[CMP_EQ]], i8 0, i8 -127
 // LLVM:  %[[CMP_GT:.*]] = fcmp ogt float %[[LHS]], %[[RHS]]
 // LLVM:  %[[SEL_GT_EQUN:.*]] = select i1 %[[CMP_GT]], i8 1, i8 %[[SEL_EQ_UN]]
+// LLVM:  %[[CMP_LT:.*]] = fcmp olt float %[[LHS]], %[[RHS]]
 // LLVM:  %[[RES:.*]] = select i1 %[[CMP_LT]], i8 -1, i8 %[[SEL_GT_EQUN]]
 
 // OGCG:  %[[LHS:.*]] = load float, ptr %{{.*}}, align 4

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

Reply via email to