Skip to content

[CIR] Upstream support for logical not operations #133966

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
Apr 5, 2025

Conversation

andykaylor
Copy link
Contributor

When unary operation support was initially upstreamed, the cir.cast operation hadn't been upstreamed yet, so logical not wasn't included. Since casts have now been added, this change adds support for logical not.

@andykaylor
Copy link
Contributor Author

@mmha

}

// Compare operand to zero.
mlir::Value boolVal = cgf.evaluateExprAsBool(e->getSubExpr());
Copy link
Collaborator

Choose a reason for hiding this comment

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

This shouldn't be necessary AFAIK. Clang should have already converted the expression via casts to bool.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wondered about that. This may be something we just blindly brought over from the classic codegen, which does the same thing. I'll try changing it there and run the regression tests to see if anything fails. If not, I'll remove it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I went to test the change, it became obvious why this is here, and I remembered that we had this same discussion a couple of weeks ago with the for-loop upstreaming. We have a boolean expression, but we need to emit a value. The AST will have an implicit cast to bool here, but EvaluateExprAsBool() does a couple of other things, like setting the current PGO statement, and doing RAII on FP operations.

I tried replacing this with emitScalarExpr in the classic codegen, and it does cause problems when the ! operator is used with floating point values. I'm not clear why it doesn't handle the implicit cast in such cases, but it doesn't.

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

Thanks for looking into that! That makes sense. LGTM.

When unary operation support was initially upstreamed, the cir.cast operation
hadn't been upstreamed yet, so logical not wasn't included. Since casts
have now been added, this change adds support for logical not.
@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Apr 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/pr-subscribers-clangir

Author: Andy Kaylor (andykaylor)

Changes

When unary operation support was initially upstreamed, the cir.cast operation hadn't been upstreamed yet, so logical not wasn't included. Since casts have now been added, this change adds support for logical not.


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

4 Files Affected:

  • (modified) clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h (+5)
  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+30-1)
  • (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+7-2)
  • (modified) clang/test/CIR/CodeGen/unary.cpp (+90)
diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 51939e3af833d..c8fa2b4f2ea7f 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -121,6 +121,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
     return cir::BoolAttr::get(getContext(), getBoolTy(), state);
   }
 
+  mlir::Value createNot(mlir::Value value) {
+    return create<cir::UnaryOp>(value.getLoc(), value.getType(),
+                                cir::UnaryOpKind::Not, value);
+  }
+
   /// Create a do-while operation.
   cir::DoWhileOp createDoWhile(
       mlir::Location loc,
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 6289a8f1d2ed7..3863d21487531 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -488,6 +488,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
     return emitUnaryOp(e, cir::UnaryOpKind::Not, op);
   }
 
+  mlir::Value VisitUnaryLNot(const UnaryOperator *e);
+
   /// Emit a conversion from the specified type to the specified destination
   /// type, both of which are CIR scalar types.
   /// TODO: do we need ScalarConversionOpts here? Should be done in another
@@ -1315,7 +1317,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
                                      "fixed point casts");
       return {};
     }
-    cgf.getCIRGenModule().errorNYI(subExpr->getSourceRange(), "fp options");
+    assert(!cir::MissingFeatures::cgFPOptionsRAII());
     return emitScalarConversion(Visit(subExpr), subExpr->getType(), destTy,
                                 ce->getExprLoc());
   }
@@ -1353,6 +1355,33 @@ mlir::Value CIRGenFunction::emitScalarConversion(mlir::Value src,
       .emitScalarConversion(src, srcTy, dstTy, loc);
 }
 
+mlir::Value ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *e) {
+  // Perform vector logical not on comparison with zero vector.
+  if (e->getType()->isVectorType() &&
+      e->getType()->castAs<VectorType>()->getVectorKind() ==
+          VectorKind::Generic) {
+    assert(!cir::MissingFeatures::vectorType());
+    cgf.cgm.errorNYI(e->getSourceRange(), "vector logical not");
+    return {};
+  }
+
+  // Compare operand to zero.
+  mlir::Value boolVal = cgf.evaluateExprAsBool(e->getSubExpr());
+
+  // Invert value.
+  boolVal = builder.createNot(boolVal);
+
+  // ZExt result to the expr type.
+  mlir::Type dstTy = cgf.convertType(e->getType());
+  if (mlir::isa<cir::IntType>(dstTy))
+    return builder.createBoolToInt(boolVal, dstTy);
+  if (mlir::isa<cir::BoolType>(dstTy))
+    return boolVal;
+
+  cgf.cgm.errorNYI("destination type for logical-not unary operator is NYI");
+  return {};
+}
+
 /// Return the size or alignment of the type of argument of the sizeof
 /// expression as an integer.
 mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 1c455039269b9..81b80e2e4eafb 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -501,9 +501,14 @@ mlir::LogicalResult CIRToLLVMCastOpLowering::matchAndRewrite(
     assert(!MissingFeatures::cxxABI());
     assert(!MissingFeatures::dataMemberType());
     break;
-  case cir::CastKind::ptr_to_bool:
-    assert(!cir::MissingFeatures::opCmp());
+  case cir::CastKind::ptr_to_bool: {
+    mlir::Value llvmSrcVal = adaptor.getOperands().front();
+    mlir::Value zeroPtr = rewriter.create<mlir::LLVM::ZeroOp>(
+        castOp.getLoc(), llvmSrcVal.getType());
+    rewriter.replaceOpWithNewOp<mlir::LLVM::ICmpOp>(
+        castOp, mlir::LLVM::ICmpPredicate::ne, llvmSrcVal, zeroPtr);
     break;
+  }
   case cir::CastKind::address_space: {
     mlir::Type dstTy = castOp.getType();
     mlir::Value llvmSrcVal = adaptor.getOperands().front();
diff --git a/clang/test/CIR/CodeGen/unary.cpp b/clang/test/CIR/CodeGen/unary.cpp
index a6405e653a07c..b0a315a18f8fb 100644
--- a/clang/test/CIR/CodeGen/unary.cpp
+++ b/clang/test/CIR/CodeGen/unary.cpp
@@ -466,3 +466,93 @@ _Float16 fp16UMinus(_Float16 f) {
 // OGCG:   %[[PROMOTED:.*]] = fpext half %[[F_LOAD]] to float
 // OGCG:   %[[RESULT:.*]] = fneg float %[[PROMOTED]]
 // OGCG:   %[[UNPROMOTED:.*]] = fptrunc float %[[RESULT]] to half
+
+void test_logical_not() {
+  int a = 5;
+  a = !a;
+  bool b = false;
+  b = !b;
+  float c = 2.0f;
+  c = !c;
+  int *p = 0;
+  b = !p;
+  double d = 3.0;
+  b = !d;
+}
+
+// CHECK: cir.func @test_logical_not()
+// CHECK:   %[[A:.*]] = cir.load %[[A_ADDR:.*]] : !cir.ptr<!s32i>, !s32i
+// CHECK:   %[[A_BOOL:.*]] = cir.cast(int_to_bool, %[[A]] : !s32i), !cir.bool
+// CHECK:   %[[A_NOT:.*]] = cir.unary(not, %[[A_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   %[[A_CAST:.*]] = cir.cast(bool_to_int, %[[A_NOT]] : !cir.bool), !s32i
+// CHECK:   cir.store %[[A_CAST]], %[[A_ADDR]] : !s32i, !cir.ptr<!s32i>
+// CHECK:   %[[B:.*]] = cir.load %[[B_ADDR:.*]] : !cir.ptr<!cir.bool>, !cir.bool
+// CHECK:   %[[B_NOT:.*]] = cir.unary(not, %[[B]]) : !cir.bool, !cir.bool
+// CHECK:   cir.store %[[B_NOT]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CHECK:   %[[C:.*]] = cir.load %[[C_ADDR:.*]] : !cir.ptr<!cir.float>, !cir.float
+// CHECK:   %[[C_BOOL:.*]] = cir.cast(float_to_bool, %[[C]] : !cir.float), !cir.bool
+// CHECK:   %[[C_NOT:.*]] = cir.unary(not, %[[C_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   %[[C_CAST:.*]] = cir.cast(bool_to_float, %[[C_NOT]] : !cir.bool), !cir.float
+// CHECK:   cir.store %[[C_CAST]], %[[C_ADDR]] : !cir.float, !cir.ptr<!cir.float>
+// CHECK:   %[[P:.*]] = cir.load %[[P_ADDR:.*]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
+// CHECK:   %[[P_BOOL:.*]] = cir.cast(ptr_to_bool, %[[P]] : !cir.ptr<!s32i>), !cir.bool
+// CHECK:   %[[P_NOT:.*]] = cir.unary(not, %[[P_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   cir.store %[[P_NOT]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CHECK:   %[[D:.*]] = cir.load %[[D_ADDR:.*]] : !cir.ptr<!cir.double>, !cir.double
+// CHECK:   %[[D_BOOL:.*]] = cir.cast(float_to_bool, %[[D]] : !cir.double), !cir.bool
+// CHECK:   %[[D_NOT:.*]] = cir.unary(not, %[[D_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   cir.store %[[D_NOT]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+
+// LLVM: define void @test_logical_not()
+// LLVM:   %[[A:.*]] = load i32, ptr %[[A_ADDR:.*]], align 4
+// LLVM:   %[[A_BOOL:.*]] = icmp ne i32 %[[A]], 0
+// LLVM:   %[[A_NOT:.*]] = xor i1 %[[A_BOOL]], true
+// LLVM:   %[[A_CAST:.*]] = zext i1 %[[A_NOT]] to i32
+// LLVM:   store i32 %[[A_CAST]], ptr %[[A_ADDR]], align 4
+// LLVM:   %[[B:.*]] = load i8, ptr %[[B_ADDR:.*]], align 1
+// LLVM:   %[[B_BOOL:.*]] = trunc i8 %[[B]] to i1
+// LLVM:   %[[B_NOT:.*]] = xor i1 %[[B_BOOL]], true
+// LLVM:   %[[B_CAST:.*]] = zext i1 %[[B_NOT]] to i8
+// LLVM:   store i8 %[[B_CAST]], ptr %[[B_ADDR]], align 1
+// LLVM:   %[[C:.*]] = load float, ptr %[[C_ADDR:.*]], align 4
+// LLVM:   %[[C_BOOL:.*]] = fcmp une float %[[C]], 0.000000e+00
+// LLVM:   %[[C_NOT:.*]] = xor i1 %[[C_BOOL]], true
+// LLVM:   %[[C_CAST:.*]] = uitofp i1 %[[C_NOT]] to float
+// LLVM:   store float %[[C_CAST]], ptr %[[C_ADDR]], align 4
+// LLVM:   %[[P:.*]] = load ptr, ptr %[[P_ADDR:.*]], align 8
+// LLVM:   %[[P_BOOL:.*]] = icmp ne ptr %[[P]], null
+// LLVM:   %[[P_NOT:.*]] = xor i1 %[[P_BOOL]], true
+// LLVM:   %[[P_CAST:.*]] = zext i1 %[[P_NOT]] to i8
+// LLVM:   store i8 %[[P_CAST]], ptr %[[B_ADDR]], align 1
+// LLVM:   %[[D:.*]] = load double, ptr %[[D_ADDR:.*]], align 8
+// LLVM:   %[[D_BOOL:.*]] = fcmp une double %[[D]], 0.000000e+00
+// LLVM:   %[[D_NOT:.*]] = xor i1 %[[D_BOOL]], true
+// LLVM:   %[[D_CAST:.*]] = zext i1 %[[D_NOT]] to i8
+// LLVM:   store i8 %[[D_CAST]], ptr %[[B_ADDR]], align 1
+
+// OGCG: define{{.*}} void @_Z16test_logical_notv()
+// OGCG:   %[[A:.*]] = load i32, ptr %[[A_ADDR:.*]], align 4
+// OGCG:   %[[A_BOOL:.*]] = icmp ne i32 %[[A]], 0
+// OGCG:   %[[A_NOT:.*]] = xor i1 %[[A_BOOL]], true
+// OGCG:   %[[A_CAST:.*]] = zext i1 %[[A_NOT]] to i32
+// OGCG:   store i32 %[[A_CAST]], ptr %[[A_ADDR]], align 4
+// OGCG:   %[[B:.*]] = load i8, ptr %[[B_ADDR:.*]], align 1
+// OGCG:   %[[B_BOOL:.*]] = trunc i8 %[[B]] to i1
+// OGCG:   %[[B_NOT:.*]] = xor i1 %[[B_BOOL]], true
+// OGCG:   %[[B_CAST:.*]] = zext i1 %[[B_NOT]] to i8
+// OGCG:   store i8 %[[B_CAST]], ptr %[[B_ADDR]], align 1
+// OGCG:   %[[C:.*]] = load float, ptr %[[C_ADDR:.*]], align 4
+// OGCG:   %[[C_BOOL:.*]] = fcmp une float %[[C]], 0.000000e+00
+// OGCG:   %[[C_NOT:.*]] = xor i1 %[[C_BOOL]], true
+// OGCG:   %[[C_CAST:.*]] = uitofp i1 %[[C_NOT]] to float
+// OGCG:   store float %[[C_CAST]], ptr %[[C_ADDR]], align 4
+// OGCG:   %[[P:.*]] = load ptr, ptr %[[P_ADDR:.*]], align 8
+// OGCG:   %[[P_BOOL:.*]] = icmp ne ptr %[[P]], null
+// OGCG:   %[[P_NOT:.*]] = xor i1 %[[P_BOOL]], true
+// OGCG:   %[[P_CAST:.*]] = zext i1 %[[P_NOT]] to i8
+// OGCG:   store i8 %[[P_CAST]], ptr %[[B_ADDR]], align 1
+// OGCG:   %[[D:.*]] = load double, ptr %[[D_ADDR:.*]], align 8
+// OGCG:   %[[D_BOOL:.*]] = fcmp une double %[[D]], 0.000000e+00
+// OGCG:   %[[D_NOT:.*]] = xor i1 %[[D_BOOL]], true
+// OGCG:   %[[D_CAST:.*]] = zext i1 %[[D_NOT]] to i8
+// OGCG:   store i8 %[[D_CAST]], ptr %[[B_ADDR]], align 1

@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2025

@llvm/pr-subscribers-clang

Author: Andy Kaylor (andykaylor)

Changes

When unary operation support was initially upstreamed, the cir.cast operation hadn't been upstreamed yet, so logical not wasn't included. Since casts have now been added, this change adds support for logical not.


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

4 Files Affected:

  • (modified) clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h (+5)
  • (modified) clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (+30-1)
  • (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+7-2)
  • (modified) clang/test/CIR/CodeGen/unary.cpp (+90)
diff --git a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
index 51939e3af833d..c8fa2b4f2ea7f 100644
--- a/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
+++ b/clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
@@ -121,6 +121,11 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
     return cir::BoolAttr::get(getContext(), getBoolTy(), state);
   }
 
+  mlir::Value createNot(mlir::Value value) {
+    return create<cir::UnaryOp>(value.getLoc(), value.getType(),
+                                cir::UnaryOpKind::Not, value);
+  }
+
   /// Create a do-while operation.
   cir::DoWhileOp createDoWhile(
       mlir::Location loc,
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 6289a8f1d2ed7..3863d21487531 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -488,6 +488,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
     return emitUnaryOp(e, cir::UnaryOpKind::Not, op);
   }
 
+  mlir::Value VisitUnaryLNot(const UnaryOperator *e);
+
   /// Emit a conversion from the specified type to the specified destination
   /// type, both of which are CIR scalar types.
   /// TODO: do we need ScalarConversionOpts here? Should be done in another
@@ -1315,7 +1317,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
                                      "fixed point casts");
       return {};
     }
-    cgf.getCIRGenModule().errorNYI(subExpr->getSourceRange(), "fp options");
+    assert(!cir::MissingFeatures::cgFPOptionsRAII());
     return emitScalarConversion(Visit(subExpr), subExpr->getType(), destTy,
                                 ce->getExprLoc());
   }
@@ -1353,6 +1355,33 @@ mlir::Value CIRGenFunction::emitScalarConversion(mlir::Value src,
       .emitScalarConversion(src, srcTy, dstTy, loc);
 }
 
+mlir::Value ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *e) {
+  // Perform vector logical not on comparison with zero vector.
+  if (e->getType()->isVectorType() &&
+      e->getType()->castAs<VectorType>()->getVectorKind() ==
+          VectorKind::Generic) {
+    assert(!cir::MissingFeatures::vectorType());
+    cgf.cgm.errorNYI(e->getSourceRange(), "vector logical not");
+    return {};
+  }
+
+  // Compare operand to zero.
+  mlir::Value boolVal = cgf.evaluateExprAsBool(e->getSubExpr());
+
+  // Invert value.
+  boolVal = builder.createNot(boolVal);
+
+  // ZExt result to the expr type.
+  mlir::Type dstTy = cgf.convertType(e->getType());
+  if (mlir::isa<cir::IntType>(dstTy))
+    return builder.createBoolToInt(boolVal, dstTy);
+  if (mlir::isa<cir::BoolType>(dstTy))
+    return boolVal;
+
+  cgf.cgm.errorNYI("destination type for logical-not unary operator is NYI");
+  return {};
+}
+
 /// Return the size or alignment of the type of argument of the sizeof
 /// expression as an integer.
 mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 1c455039269b9..81b80e2e4eafb 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -501,9 +501,14 @@ mlir::LogicalResult CIRToLLVMCastOpLowering::matchAndRewrite(
     assert(!MissingFeatures::cxxABI());
     assert(!MissingFeatures::dataMemberType());
     break;
-  case cir::CastKind::ptr_to_bool:
-    assert(!cir::MissingFeatures::opCmp());
+  case cir::CastKind::ptr_to_bool: {
+    mlir::Value llvmSrcVal = adaptor.getOperands().front();
+    mlir::Value zeroPtr = rewriter.create<mlir::LLVM::ZeroOp>(
+        castOp.getLoc(), llvmSrcVal.getType());
+    rewriter.replaceOpWithNewOp<mlir::LLVM::ICmpOp>(
+        castOp, mlir::LLVM::ICmpPredicate::ne, llvmSrcVal, zeroPtr);
     break;
+  }
   case cir::CastKind::address_space: {
     mlir::Type dstTy = castOp.getType();
     mlir::Value llvmSrcVal = adaptor.getOperands().front();
diff --git a/clang/test/CIR/CodeGen/unary.cpp b/clang/test/CIR/CodeGen/unary.cpp
index a6405e653a07c..b0a315a18f8fb 100644
--- a/clang/test/CIR/CodeGen/unary.cpp
+++ b/clang/test/CIR/CodeGen/unary.cpp
@@ -466,3 +466,93 @@ _Float16 fp16UMinus(_Float16 f) {
 // OGCG:   %[[PROMOTED:.*]] = fpext half %[[F_LOAD]] to float
 // OGCG:   %[[RESULT:.*]] = fneg float %[[PROMOTED]]
 // OGCG:   %[[UNPROMOTED:.*]] = fptrunc float %[[RESULT]] to half
+
+void test_logical_not() {
+  int a = 5;
+  a = !a;
+  bool b = false;
+  b = !b;
+  float c = 2.0f;
+  c = !c;
+  int *p = 0;
+  b = !p;
+  double d = 3.0;
+  b = !d;
+}
+
+// CHECK: cir.func @test_logical_not()
+// CHECK:   %[[A:.*]] = cir.load %[[A_ADDR:.*]] : !cir.ptr<!s32i>, !s32i
+// CHECK:   %[[A_BOOL:.*]] = cir.cast(int_to_bool, %[[A]] : !s32i), !cir.bool
+// CHECK:   %[[A_NOT:.*]] = cir.unary(not, %[[A_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   %[[A_CAST:.*]] = cir.cast(bool_to_int, %[[A_NOT]] : !cir.bool), !s32i
+// CHECK:   cir.store %[[A_CAST]], %[[A_ADDR]] : !s32i, !cir.ptr<!s32i>
+// CHECK:   %[[B:.*]] = cir.load %[[B_ADDR:.*]] : !cir.ptr<!cir.bool>, !cir.bool
+// CHECK:   %[[B_NOT:.*]] = cir.unary(not, %[[B]]) : !cir.bool, !cir.bool
+// CHECK:   cir.store %[[B_NOT]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CHECK:   %[[C:.*]] = cir.load %[[C_ADDR:.*]] : !cir.ptr<!cir.float>, !cir.float
+// CHECK:   %[[C_BOOL:.*]] = cir.cast(float_to_bool, %[[C]] : !cir.float), !cir.bool
+// CHECK:   %[[C_NOT:.*]] = cir.unary(not, %[[C_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   %[[C_CAST:.*]] = cir.cast(bool_to_float, %[[C_NOT]] : !cir.bool), !cir.float
+// CHECK:   cir.store %[[C_CAST]], %[[C_ADDR]] : !cir.float, !cir.ptr<!cir.float>
+// CHECK:   %[[P:.*]] = cir.load %[[P_ADDR:.*]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
+// CHECK:   %[[P_BOOL:.*]] = cir.cast(ptr_to_bool, %[[P]] : !cir.ptr<!s32i>), !cir.bool
+// CHECK:   %[[P_NOT:.*]] = cir.unary(not, %[[P_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   cir.store %[[P_NOT]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+// CHECK:   %[[D:.*]] = cir.load %[[D_ADDR:.*]] : !cir.ptr<!cir.double>, !cir.double
+// CHECK:   %[[D_BOOL:.*]] = cir.cast(float_to_bool, %[[D]] : !cir.double), !cir.bool
+// CHECK:   %[[D_NOT:.*]] = cir.unary(not, %[[D_BOOL]]) : !cir.bool, !cir.bool
+// CHECK:   cir.store %[[D_NOT]], %[[B_ADDR]] : !cir.bool, !cir.ptr<!cir.bool>
+
+// LLVM: define void @test_logical_not()
+// LLVM:   %[[A:.*]] = load i32, ptr %[[A_ADDR:.*]], align 4
+// LLVM:   %[[A_BOOL:.*]] = icmp ne i32 %[[A]], 0
+// LLVM:   %[[A_NOT:.*]] = xor i1 %[[A_BOOL]], true
+// LLVM:   %[[A_CAST:.*]] = zext i1 %[[A_NOT]] to i32
+// LLVM:   store i32 %[[A_CAST]], ptr %[[A_ADDR]], align 4
+// LLVM:   %[[B:.*]] = load i8, ptr %[[B_ADDR:.*]], align 1
+// LLVM:   %[[B_BOOL:.*]] = trunc i8 %[[B]] to i1
+// LLVM:   %[[B_NOT:.*]] = xor i1 %[[B_BOOL]], true
+// LLVM:   %[[B_CAST:.*]] = zext i1 %[[B_NOT]] to i8
+// LLVM:   store i8 %[[B_CAST]], ptr %[[B_ADDR]], align 1
+// LLVM:   %[[C:.*]] = load float, ptr %[[C_ADDR:.*]], align 4
+// LLVM:   %[[C_BOOL:.*]] = fcmp une float %[[C]], 0.000000e+00
+// LLVM:   %[[C_NOT:.*]] = xor i1 %[[C_BOOL]], true
+// LLVM:   %[[C_CAST:.*]] = uitofp i1 %[[C_NOT]] to float
+// LLVM:   store float %[[C_CAST]], ptr %[[C_ADDR]], align 4
+// LLVM:   %[[P:.*]] = load ptr, ptr %[[P_ADDR:.*]], align 8
+// LLVM:   %[[P_BOOL:.*]] = icmp ne ptr %[[P]], null
+// LLVM:   %[[P_NOT:.*]] = xor i1 %[[P_BOOL]], true
+// LLVM:   %[[P_CAST:.*]] = zext i1 %[[P_NOT]] to i8
+// LLVM:   store i8 %[[P_CAST]], ptr %[[B_ADDR]], align 1
+// LLVM:   %[[D:.*]] = load double, ptr %[[D_ADDR:.*]], align 8
+// LLVM:   %[[D_BOOL:.*]] = fcmp une double %[[D]], 0.000000e+00
+// LLVM:   %[[D_NOT:.*]] = xor i1 %[[D_BOOL]], true
+// LLVM:   %[[D_CAST:.*]] = zext i1 %[[D_NOT]] to i8
+// LLVM:   store i8 %[[D_CAST]], ptr %[[B_ADDR]], align 1
+
+// OGCG: define{{.*}} void @_Z16test_logical_notv()
+// OGCG:   %[[A:.*]] = load i32, ptr %[[A_ADDR:.*]], align 4
+// OGCG:   %[[A_BOOL:.*]] = icmp ne i32 %[[A]], 0
+// OGCG:   %[[A_NOT:.*]] = xor i1 %[[A_BOOL]], true
+// OGCG:   %[[A_CAST:.*]] = zext i1 %[[A_NOT]] to i32
+// OGCG:   store i32 %[[A_CAST]], ptr %[[A_ADDR]], align 4
+// OGCG:   %[[B:.*]] = load i8, ptr %[[B_ADDR:.*]], align 1
+// OGCG:   %[[B_BOOL:.*]] = trunc i8 %[[B]] to i1
+// OGCG:   %[[B_NOT:.*]] = xor i1 %[[B_BOOL]], true
+// OGCG:   %[[B_CAST:.*]] = zext i1 %[[B_NOT]] to i8
+// OGCG:   store i8 %[[B_CAST]], ptr %[[B_ADDR]], align 1
+// OGCG:   %[[C:.*]] = load float, ptr %[[C_ADDR:.*]], align 4
+// OGCG:   %[[C_BOOL:.*]] = fcmp une float %[[C]], 0.000000e+00
+// OGCG:   %[[C_NOT:.*]] = xor i1 %[[C_BOOL]], true
+// OGCG:   %[[C_CAST:.*]] = uitofp i1 %[[C_NOT]] to float
+// OGCG:   store float %[[C_CAST]], ptr %[[C_ADDR]], align 4
+// OGCG:   %[[P:.*]] = load ptr, ptr %[[P_ADDR:.*]], align 8
+// OGCG:   %[[P_BOOL:.*]] = icmp ne ptr %[[P]], null
+// OGCG:   %[[P_NOT:.*]] = xor i1 %[[P_BOOL]], true
+// OGCG:   %[[P_CAST:.*]] = zext i1 %[[P_NOT]] to i8
+// OGCG:   store i8 %[[P_CAST]], ptr %[[B_ADDR]], align 1
+// OGCG:   %[[D:.*]] = load double, ptr %[[D_ADDR:.*]], align 8
+// OGCG:   %[[D_BOOL:.*]] = fcmp une double %[[D]], 0.000000e+00
+// OGCG:   %[[D_NOT:.*]] = xor i1 %[[D_BOOL]], true
+// OGCG:   %[[D_CAST:.*]] = zext i1 %[[D_NOT]] to i8
+// OGCG:   store i8 %[[D_CAST]], ptr %[[B_ADDR]], align 1

@andykaylor andykaylor merged commit 78905ce into llvm:main Apr 5, 2025
13 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Apr 5, 2025

LLVM Buildbot has detected a new failure on builder clang-aarch64-sve2-vla-2stage running on linaro-g4-02 while building clang at step 12 "ninja check 2".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/199/builds/2588

Here is the relevant piece of the build log for the reference
Step 12 (ninja check 2) failure: stage 2 checked (failure)
...
PASS: Flang :: Driver/linker-flags.f90 (24782 of 96614)
PASS: Flang :: Driver/override-triple.ll (24783 of 96614)
PASS: Flang :: Driver/print-pipeline-passes.f90 (24784 of 96614)
PASS: Flang :: Driver/predefined-macros-compiler-version.F90 (24785 of 96614)
PASS: Flang :: Driver/print-resource-dir.F90 (24786 of 96614)
PASS: Flang :: Driver/fixed-line-length.f90 (24787 of 96614)
PASS: Flang :: Driver/phases.f90 (24788 of 96614)
PASS: Flang :: Driver/parse-error.ll (24789 of 96614)
PASS: Flang :: Driver/parse-fir-error.ll (24790 of 96614)
UNRESOLVED: Flang :: Driver/slp-vectorize.ll (24791 of 96614)
******************** TEST 'Flang :: Driver/slp-vectorize.ll' FAILED ********************
Test has no 'RUN:' line
********************
PASS: Flang :: Driver/macro-def-undef.F90 (24792 of 96614)
PASS: Flang :: Driver/print-target-triple.f90 (24793 of 96614)
PASS: Flang :: Driver/mlink-builtin-bc.f90 (24794 of 96614)
PASS: Flang :: Driver/pthread.f90 (24795 of 96614)
PASS: Flang :: Driver/lto-bc.f90 (24796 of 96614)
PASS: Flang :: Driver/parse-ir-error.f95 (24797 of 96614)
PASS: Flang :: Driver/scanning-error.f95 (24798 of 96614)
PASS: Flang :: Driver/std2018-wrong.f90 (24799 of 96614)
PASS: Flang :: Driver/missing-arg.f90 (24800 of 96614)
PASS: Flang :: Driver/mlir-pass-pipeline.f90 (24801 of 96614)
PASS: Flang :: Driver/supported-suffices/f03-suffix.f03 (24802 of 96614)
PASS: Flang :: Driver/pp-fixed-form.f90 (24803 of 96614)
PASS: Flang :: Driver/supported-suffices/f08-suffix.f08 (24804 of 96614)
PASS: Flang :: Driver/pass-plugin-not-found.f90 (24805 of 96614)
PASS: Clangd Unit Tests :: ./ClangdTests/73/81 (24806 of 96614)
PASS: Flang :: Driver/tco-code-gen-llvm.fir (24807 of 96614)
PASS: Flang :: Driver/target-gpu-features.f90 (24808 of 96614)
PASS: Flang :: Driver/q-unused-arguments.f90 (24809 of 96614)
PASS: Flang :: Driver/mllvm.f90 (24810 of 96614)
PASS: Flang :: Driver/fsave-optimization-record.f90 (24811 of 96614)
PASS: Flang :: Driver/target.f90 (24812 of 96614)
PASS: Flang :: Driver/lto-flags.f90 (24813 of 96614)
PASS: Flang :: Driver/unsupported-vscale-max-min.f90 (24814 of 96614)
PASS: Flang :: Driver/multiple-input-files.f90 (24815 of 96614)
PASS: Flang :: Driver/no-duplicate-main.f90 (24816 of 96614)
PASS: Flang :: Driver/input-from-stdin/input-from-stdin.f90 (24817 of 96614)
PASS: Flang :: Driver/std2018.f90 (24818 of 96614)
PASS: Flang :: Driver/unparse-with-modules.f90 (24819 of 96614)
PASS: Flang :: Driver/save-temps.f90 (24820 of 96614)
PASS: Flang :: Driver/unparse-use-analyzed.f95 (24821 of 96614)
PASS: Flang :: Driver/prescanner-diag.f90 (24822 of 96614)
PASS: Flang :: Driver/optimization-remark-invalid.f90 (24823 of 96614)
PASS: Flang :: Driver/save-temps-use-module.f90 (24824 of 96614)
PASS: Flang :: Driver/target-machine-error.f90 (24825 of 96614)
PASS: Flang :: Driver/target-cpu-features-invalid.f90 (24826 of 96614)
PASS: Flang :: Driver/falias-analysis.f90 (24827 of 96614)

@andykaylor andykaylor deleted the cir-unary-not branch April 10, 2025 21:17
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