[llvm-branch-commits] [clang] [CIR] Add InlineAsmOp lowering to LLVM (PR #153387)

2025-08-14 Thread Morris Hafner via llvm-branch-commits

https://github.com/mmha edited https://github.com/llvm/llvm-project/pull/153387
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Add InlineAsmOp lowering to LLVM (PR #153387)

2025-08-14 Thread Morris Hafner via llvm-branch-commits


@@ -2896,6 +2898,68 @@ mlir::LogicalResult 
CIRToLLVMGetBitfieldOpLowering::matchAndRewrite(
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
+cir::InlineAsmOp op, OpAdaptor adaptor,
+mlir::ConversionPatternRewriter &rewriter) const {
+  mlir::Type llResTy;
+  if (op.getNumResults())
+llResTy = getTypeConverter()->convertType(op.getType(0));
+
+  cir::AsmFlavor dialect = op.getAsmFlavor();
+  mlir::LLVM::AsmDialect llDialect = dialect == cir::AsmFlavor::x86_att
+ ? mlir::LLVM::AsmDialect::AD_ATT
+ : mlir::LLVM::AsmDialect::AD_Intel;
+
+  SmallVector opAttrs;
+  StringRef llvmAttrName = mlir::LLVM::InlineAsmOp::getElementTypeAttrName();
+
+  // this is for the lowering to LLVM from LLVM dialect. Otherwise, if we
+  // don't have the result (i.e. void type as a result of operation), the
+  // element type attribute will be attached to the whole instruction, but not
+  // to the operand
+  if (!op.getNumResults())
+opAttrs.push_back(mlir::Attribute());
+
+  SmallVector llvmOperands;
+  SmallVector cirOperands;
+  for (auto [llvmOp, cirOp] :
+   llvm::zip(adaptor.getAsmOperands(), op.getAsmOperands())) {
+llvmOperands.append(llvmOp.begin(), llvmOp.end());
+cirOperands.append(cirOp.begin(), cirOp.end());

mmha wrote:

```suggestion
append_range(llvmOperands, llvmOp);
append_range(cirOperands, cirOp);
```

https://github.com/llvm/llvm-project/pull/153387
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Add InlineAsmOp lowering to LLVM (PR #153387)

2025-08-14 Thread Morris Hafner via llvm-branch-commits


@@ -2896,6 +2898,68 @@ mlir::LogicalResult 
CIRToLLVMGetBitfieldOpLowering::matchAndRewrite(
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
+cir::InlineAsmOp op, OpAdaptor adaptor,
+mlir::ConversionPatternRewriter &rewriter) const {
+  mlir::Type llResTy;
+  if (op.getNumResults())
+llResTy = getTypeConverter()->convertType(op.getType(0));
+
+  cir::AsmFlavor dialect = op.getAsmFlavor();
+  mlir::LLVM::AsmDialect llDialect = dialect == cir::AsmFlavor::x86_att
+ ? mlir::LLVM::AsmDialect::AD_ATT
+ : mlir::LLVM::AsmDialect::AD_Intel;
+
+  SmallVector opAttrs;
+  StringRef llvmAttrName = mlir::LLVM::InlineAsmOp::getElementTypeAttrName();
+
+  // this is for the lowering to LLVM from LLVM dialect. Otherwise, if we
+  // don't have the result (i.e. void type as a result of operation), the
+  // element type attribute will be attached to the whole instruction, but not
+  // to the operand
+  if (!op.getNumResults())
+opAttrs.push_back(mlir::Attribute());
+
+  SmallVector llvmOperands;
+  SmallVector cirOperands;
+  for (auto [llvmOp, cirOp] :
+   llvm::zip(adaptor.getAsmOperands(), op.getAsmOperands())) {
+llvmOperands.append(llvmOp.begin(), llvmOp.end());
+cirOperands.append(cirOp.begin(), cirOp.end());
+  }
+
+  // so far we infer the llvm dialect element type attr from
+  // CIR operand type.
+  for (auto [i, cirOpAttr] : llvm::enumerate(op.getOperandAttrs())) {
+if (!cirOpAttr) {
+  opAttrs.push_back(mlir::Attribute());
+  continue;
+}
+
+SmallVector attrs;
+cir::PointerType typ =
+mlir::cast(cirOperands[i].getType());

mmha wrote:

```suggestion
mlir::cast(cirOp.getType());
```

https://github.com/llvm/llvm-project/pull/153387
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Add InlineAsmOp lowering to LLVM (PR #153387)

2025-08-14 Thread Morris Hafner via llvm-branch-commits


@@ -2896,6 +2898,68 @@ mlir::LogicalResult 
CIRToLLVMGetBitfieldOpLowering::matchAndRewrite(
   return mlir::success();
 }
 
+mlir::LogicalResult CIRToLLVMInlineAsmOpLowering::matchAndRewrite(
+cir::InlineAsmOp op, OpAdaptor adaptor,
+mlir::ConversionPatternRewriter &rewriter) const {
+  mlir::Type llResTy;
+  if (op.getNumResults())
+llResTy = getTypeConverter()->convertType(op.getType(0));
+
+  cir::AsmFlavor dialect = op.getAsmFlavor();
+  mlir::LLVM::AsmDialect llDialect = dialect == cir::AsmFlavor::x86_att
+ ? mlir::LLVM::AsmDialect::AD_ATT
+ : mlir::LLVM::AsmDialect::AD_Intel;
+
+  SmallVector opAttrs;
+  StringRef llvmAttrName = mlir::LLVM::InlineAsmOp::getElementTypeAttrName();
+
+  // this is for the lowering to LLVM from LLVM dialect. Otherwise, if we
+  // don't have the result (i.e. void type as a result of operation), the
+  // element type attribute will be attached to the whole instruction, but not
+  // to the operand
+  if (!op.getNumResults())
+opAttrs.push_back(mlir::Attribute());
+
+  SmallVector llvmOperands;
+  SmallVector cirOperands;
+  for (auto [llvmOp, cirOp] :
+   llvm::zip(adaptor.getAsmOperands(), op.getAsmOperands())) {
+llvmOperands.append(llvmOp.begin(), llvmOp.end());
+cirOperands.append(cirOp.begin(), cirOp.end());
+  }
+
+  // so far we infer the llvm dialect element type attr from
+  // CIR operand type.
+  for (auto [i, cirOpAttr] : llvm::enumerate(op.getOperandAttrs())) {

mmha wrote:

```suggestion
  for (auto const&[cirOpAttr, cirOp] : zip(op.getOperandAttrs(), cirOperands)) {
```

https://github.com/llvm/llvm-project/pull/153387
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Add InlineAsmOp lowering to LLVM (PR #153387)

2025-08-14 Thread Morris Hafner via llvm-branch-commits

https://github.com/mmha approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/153387
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Add support for string literal lvalues in ConstantLValueEmitter (PR #154360)

2025-08-19 Thread Morris Hafner via llvm-branch-commits

https://github.com/mmha edited https://github.com/llvm/llvm-project/pull/154360
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Add support for string literal lvalues in ConstantLValueEmitter (PR #154360)

2025-08-19 Thread Morris Hafner via llvm-branch-commits

https://github.com/mmha updated https://github.com/llvm/llvm-project/pull/154360

>From d592acb40bfcea7b9e3eca1ac29ecab96315d26f Mon Sep 17 00:00:00 2001
From: Morris Hafner 
Date: Tue, 19 Aug 2025 17:13:51 +0200
Subject: [PATCH 1/2] [CIR] Add support for string literal lavlues in
 ConstantLValueEmitter

---
 clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp |  3 +-
 clang/lib/CIR/CodeGen/CIRGenModule.cpp   | 13 
 clang/lib/CIR/CodeGen/CIRGenModule.h |  6 
 clang/test/CIR/CodeGen/string-literals.cpp   | 34 +---
 4 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
index 2fbf69d5d01f4..d26269eb93d3f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
@@ -546,8 +546,7 @@ ConstantLValueEmitter::VisitCompoundLiteralExpr(const 
CompoundLiteralExpr *e) {
 
 ConstantLValue
 ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *e) {
-  cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: string literal");
-  return {};
+  return cgm.getAddrOfConstantStringFromLiteral(e);
 }
 
 ConstantLValue
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp 
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 7c52adcfa56c7..cb8cc30835f62 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1318,6 +1318,19 @@ cir::GlobalOp 
CIRGenModule::getGlobalForStringLiteral(const StringLiteral *s,
   return gv;
 }
 
+/// Return a pointer to a constant array for the given string literal.
+cir::GlobalViewAttr
+CIRGenModule::getAddrOfConstantStringFromLiteral(const StringLiteral *s,
+ StringRef name) {
+  cir::GlobalOp gv = getGlobalForStringLiteral(s, name);
+  auto arrayTy = mlir::dyn_cast(gv.getSymType());
+  assert(arrayTy && "String literal must be array");
+  assert(!cir::MissingFeatures::addressSpace());
+  cir::PointerType ptrTy = getBuilder().getPointerTo(arrayTy.getElementType());
+
+  return builder.getGlobalViewAttr(ptrTy, gv);
+}
+
 void CIRGenModule::emitExplicitCastExprType(const ExplicitCastExpr *e,
 CIRGenFunction *cgf) {
   if (cgf && e->getType()->isVariablyModifiedType())
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h 
b/clang/lib/CIR/CodeGen/CIRGenModule.h
index ce9d0344aa940..769857ab49bb8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.h
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -198,6 +198,12 @@ class CIRGenModule : public CIRGenTypeCache {
   cir::GlobalOp getGlobalForStringLiteral(const StringLiteral *s,
   llvm::StringRef name = ".str");
 
+  /// Return a global symbol reference to a constant array for the given string
+  /// literal.
+  cir::GlobalViewAttr
+  getAddrOfConstantStringFromLiteral(const StringLiteral *s,
+ llvm::StringRef name = ".str");
+
   /// Set attributes which are common to any form of a global definition 
(alias,
   /// Objective-C method, function, global variable).
   ///
diff --git a/clang/test/CIR/CodeGen/string-literals.cpp 
b/clang/test/CIR/CodeGen/string-literals.cpp
index a20add08bfc2d..14115672e28e2 100644
--- a/clang/test/CIR/CodeGen/string-literals.cpp
+++ b/clang/test/CIR/CodeGen/string-literals.cpp
@@ -5,11 +5,37 @@
 // RUN: %clang_cc1 -triple aarch64-none-linux-android21 -emit-llvm %s -o %t.ll
 // RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
 
-// CIR: cir.global "private" constant cir_private dso_local 
@[[STR1_GLOBAL:.*]] = #cir.const_array<"abcd\00" : !cir.array> : 
!cir.array
 
-// LLVM: @[[STR1_GLOBAL:.*]] = private constant [5 x i8] c"abcd\00"
+char const *array[] {
+"my", "hands", "are", "typing", "words"
+};
 
-// OGCG: @[[STR1_GLOBAL:.*]] = private unnamed_addr constant [5 x i8] 
c"abcd\00"
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR:.+]]" = 
#cir.const_array<"my\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR1:.+]]" = 
#cir.const_array<"hands\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR2:.+]]" = 
#cir.const_array<"are\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR3:.+]]" = 
#cir.const_array<"typing\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR4:.+]]" = 
#cir.const_array<"words\00" : !cir.array> : !cir.array
+// CIR: cir.global external @array = 
#cir.const_array<[#cir.global_view<@"[[STR]]"> : !cir.ptr, 
#cir.global_view<@"[[STR1]]"> : !cir.ptr, #cir.global_view<@"[[STR2]]"> : 
!cir.ptr, #cir.global_view<@"[[STR3]]"> : !cir.ptr, 
#cir.global_view<@"[[STR4]]"> : !cir.ptr]> : !cir.array x 
5>
+
+// LLVM: @[[STR:.+]] = private constant [3 x i8] c"my\00"
+// LLVM: @[[STR1:.+]] = private constant [6 x i8] 

[llvm-branch-commits] [clang] [CIR] Add support for string literal lavlues in ConstantLValueEmitter (PR #154360)

2025-08-19 Thread Morris Hafner via llvm-branch-commits

https://github.com/mmha created https://github.com/llvm/llvm-project/pull/154360

Depends on #154359.

>From d592acb40bfcea7b9e3eca1ac29ecab96315d26f Mon Sep 17 00:00:00 2001
From: Morris Hafner 
Date: Tue, 19 Aug 2025 17:13:51 +0200
Subject: [PATCH] [CIR] Add support for string literal lavlues in
 ConstantLValueEmitter

---
 clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp |  3 +-
 clang/lib/CIR/CodeGen/CIRGenModule.cpp   | 13 
 clang/lib/CIR/CodeGen/CIRGenModule.h |  6 
 clang/test/CIR/CodeGen/string-literals.cpp   | 34 +---
 4 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
index 2fbf69d5d01f4..d26269eb93d3f 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
@@ -546,8 +546,7 @@ ConstantLValueEmitter::VisitCompoundLiteralExpr(const 
CompoundLiteralExpr *e) {
 
 ConstantLValue
 ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *e) {
-  cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: string literal");
-  return {};
+  return cgm.getAddrOfConstantStringFromLiteral(e);
 }
 
 ConstantLValue
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp 
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index 7c52adcfa56c7..cb8cc30835f62 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1318,6 +1318,19 @@ cir::GlobalOp 
CIRGenModule::getGlobalForStringLiteral(const StringLiteral *s,
   return gv;
 }
 
+/// Return a pointer to a constant array for the given string literal.
+cir::GlobalViewAttr
+CIRGenModule::getAddrOfConstantStringFromLiteral(const StringLiteral *s,
+ StringRef name) {
+  cir::GlobalOp gv = getGlobalForStringLiteral(s, name);
+  auto arrayTy = mlir::dyn_cast(gv.getSymType());
+  assert(arrayTy && "String literal must be array");
+  assert(!cir::MissingFeatures::addressSpace());
+  cir::PointerType ptrTy = getBuilder().getPointerTo(arrayTy.getElementType());
+
+  return builder.getGlobalViewAttr(ptrTy, gv);
+}
+
 void CIRGenModule::emitExplicitCastExprType(const ExplicitCastExpr *e,
 CIRGenFunction *cgf) {
   if (cgf && e->getType()->isVariablyModifiedType())
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h 
b/clang/lib/CIR/CodeGen/CIRGenModule.h
index ce9d0344aa940..769857ab49bb8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.h
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -198,6 +198,12 @@ class CIRGenModule : public CIRGenTypeCache {
   cir::GlobalOp getGlobalForStringLiteral(const StringLiteral *s,
   llvm::StringRef name = ".str");
 
+  /// Return a global symbol reference to a constant array for the given string
+  /// literal.
+  cir::GlobalViewAttr
+  getAddrOfConstantStringFromLiteral(const StringLiteral *s,
+ llvm::StringRef name = ".str");
+
   /// Set attributes which are common to any form of a global definition 
(alias,
   /// Objective-C method, function, global variable).
   ///
diff --git a/clang/test/CIR/CodeGen/string-literals.cpp 
b/clang/test/CIR/CodeGen/string-literals.cpp
index a20add08bfc2d..14115672e28e2 100644
--- a/clang/test/CIR/CodeGen/string-literals.cpp
+++ b/clang/test/CIR/CodeGen/string-literals.cpp
@@ -5,11 +5,37 @@
 // RUN: %clang_cc1 -triple aarch64-none-linux-android21 -emit-llvm %s -o %t.ll
 // RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
 
-// CIR: cir.global "private" constant cir_private dso_local 
@[[STR1_GLOBAL:.*]] = #cir.const_array<"abcd\00" : !cir.array> : 
!cir.array
 
-// LLVM: @[[STR1_GLOBAL:.*]] = private constant [5 x i8] c"abcd\00"
+char const *array[] {
+"my", "hands", "are", "typing", "words"
+};
 
-// OGCG: @[[STR1_GLOBAL:.*]] = private unnamed_addr constant [5 x i8] 
c"abcd\00"
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR:.+]]" = 
#cir.const_array<"my\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR1:.+]]" = 
#cir.const_array<"hands\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR2:.+]]" = 
#cir.const_array<"are\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR3:.+]]" = 
#cir.const_array<"typing\00" : !cir.array> : !cir.array
+// CIR: cir.global "private" constant cir_private dso_local @"[[STR4:.+]]" = 
#cir.const_array<"words\00" : !cir.array> : !cir.array
+// CIR: cir.global external @array = 
#cir.const_array<[#cir.global_view<@"[[STR]]"> : !cir.ptr, 
#cir.global_view<@"[[STR1]]"> : !cir.ptr, #cir.global_view<@"[[STR2]]"> : 
!cir.ptr, #cir.global_view<@"[[STR3]]"> : !cir.ptr, 
#cir.global_view<@"[[STR4]]"> : !cir.ptr]> : !cir.array x 
5>
+
+// LLVM: @[[STR:.+]] = private constant [3 x i8] c"my\00"
+// LLVM: @[[STR1:.+]] = private c

[llvm-branch-commits] [clang] [CIR] Add support for string literal lvalues in ConstantLValueEmitter (PR #154360)

2025-08-20 Thread Morris Hafner via llvm-branch-commits

https://github.com/mmha closed https://github.com/llvm/llvm-project/pull/154360
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits