https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/229879
>From afe1163fddd13810781331afc8e7944d054541c2 Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Wed, 7 Oct 2026 21:08:52 +0200 Subject: [PATCH 1/4] [CIR] Implement builtin MatrixExtract operation --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 39 +++++++++++++++++++ clang/lib/CIR/CodeGen/CIRGenBuilder.h | 7 ++++ clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 6 +++ clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 8 ++-- clang/lib/CIR/CodeGen/CIRGenFunction.h | 2 + .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 22 +++++++++++ clang/test/CIR/CodeGen/matrix.cpp | 19 +++++++++ 7 files changed, 100 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index d46b9135e618cf..36689745acd8fc 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -6417,6 +6417,45 @@ def CIR_VecSplatOp : CIR_Op<"vec.splat", [ }]; } +//===----------------------------------------------------------------------===// +// MatrixExtractOp +//===----------------------------------------------------------------------===// + +def CIR_MatrixExtractOp : CIR_Op<"matrix.extract", [ + Pure, + TypesMatchWith<"type of 'result' matches element type of 'matrix'", + "matrix", "result", "mlir::cast<cir::MatrixType>($_self).getElementType()"> +]> { + let summary = "Extract one element from a matrix object"; + let description = [{ + The `cir.matrix.extract` operation extracts the element at the given row + and column indices from a matrix object. + + Currently, the flatten index is a column-based index. + + ``` + %rowIdx = cir.const #cir.int<1> : !u64i + %columnIdx = cir.const #cir.int<2> : !u64i + %element = cir.matrix.extract %matrix[%rowIdx : !u64i, %columnIdx : !u64i] + : !cir.matrix<3 x 3 x !cir.float> + ``` + }]; + + let arguments = (ins + CIR_MatrixType:$matrix, + CIR_AnyFundamentalIntType:$rowIdx, + CIR_AnyFundamentalIntType:$columnIdx + ); + + let results = (outs CIR_MatrixElementType:$result); + + let assemblyFormat = [{ + $matrix + `[` $rowIdx `:` type($rowIdx) `,` $columnIdx `:` type($columnIdx) `]` + attr-dict `:` qualified(type($matrix)) + }]; +} + //===----------------------------------------------------------------------===// // MatrixColumnMajorLoadOp //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h b/clang/lib/CIR/CodeGen/CIRGenBuilder.h index b3b3805e8bb2b0..fec69f96a808d7 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h +++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h @@ -826,6 +826,13 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { return createVecShuffle(loc, vec1, poison, mask); } + cir::MatrixExtractOp createMatrixExtract(mlir::Location loc, + mlir::Value matrix, + mlir::Value rowIdx, + mlir::Value columnIdx) { + return cir::MatrixExtractOp::create(*this, loc, matrix, rowIdx, columnIdx); + } + cir::MatrixColumnMajorLoadOp createMatrixColumnMajorLoad(mlir::Location loc, mlir::Type resultTy, mlir::Value value, diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index c03f7b355b6959..230edb0f9de06f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -1552,6 +1552,12 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) { return lv; } +mlir::Value CIRGenFunction::emitMatrixIndexExpr(const Expr *e) { + mlir::Value idx = emitScalarExpr(e); + return idx.getType() == uIntPtrTy ? idx + : builder.createIntCast(idx, uIntPtrTy); +} + LValue CIRGenFunction::emitExtVectorElementExpr(const ExtVectorElementExpr *e) { // Emit the base vector as an l-value. LValue base; diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 836f27cf5345ef..a9cbdac30f0f8d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -320,9 +320,11 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> { } mlir::Value VisitMatrixSubscriptExpr(MatrixSubscriptExpr *e) { - cgf.cgm.errorNYI(e->getSourceRange(), - "ScalarExprEmitter: matrix subscript"); - return {}; + mlir::Value matrix = Visit(e->getBase()); + mlir::Value rowIdx = cgf.emitMatrixIndexExpr(e->getRowIdx()); + mlir::Value columnIdx = cgf.emitMatrixIndexExpr(e->getColumnIdx()); + mlir::Location loc = cgf.getLoc(e->getSourceRange()); + return builder.createMatrixExtract(loc, matrix, rowIdx, columnIdx); } mlir::Value VisitMatrixSingleSubscriptExpr(MatrixSingleSubscriptExpr *e) { diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index 6db6a232b9ece8..39bb3b7e8dea2e 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -1854,6 +1854,8 @@ class CIRGenFunction : public CIRGenTypeCache { LValue emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e); LValue emitInitListLValue(const InitListExpr *e); + mlir::Value emitMatrixIndexExpr(const Expr *e); + LValue emitExtVectorElementExpr(const ExtVectorElementExpr *e); Address emitArrayToPointerDecay(const Expr *e, diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index cd7d7606776300..f0d75eb6fced8c 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -5271,6 +5271,28 @@ mlir::LogicalResult CIRToLLVMVecTernaryOpLowering::matchAndRewrite( return mlir::success(); } +mlir::LogicalResult CIRToLLVMMatrixExtractOpLowering::matchAndRewrite( + cir::MatrixExtractOp op, OpAdaptor adaptor, + mlir::ConversionPatternRewriter &rewriter) const { + cir::MatrixType matrixTy = op.getMatrix().getType(); + mlir::Value rowIdx = adaptor.getRowIdx(); + mlir::Value columnIdx = adaptor.getColumnIdx(); + mlir::Type indexTy = rowIdx.getType(); + mlir::Value numRows = mlir::LLVM::ConstantOp::create( + rewriter, op.getLoc(), indexTy, + rewriter.getI64IntegerAttr(matrixTy.getRowNum())); + + // Column major index = (columnIdx * numRows) + rowIdx + mlir::Value flatIndex = mlir::LLVM::MulOp::create( + rewriter, op->getLoc(), {columnIdx, numRows}, /*properties'clang=*/{}, + /*discardableAttributes=*/{}); + flatIndex = mlir::LLVM::AddOp::create(rewriter, op->getLoc(), flatIndex, + rowIdx, /*overflowFlags=*/{}); + rewriter.replaceOpWithNewOp<mlir::LLVM::ExtractElementOp>( + op, adaptor.getMatrix(), flatIndex); + return mlir::success(); +} + mlir::LogicalResult CIRToLLVMMatrixColumnMajorLoadOpLowering::matchAndRewrite( cir::MatrixColumnMajorLoadOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { diff --git a/clang/test/CIR/CodeGen/matrix.cpp b/clang/test/CIR/CodeGen/matrix.cpp index b1aa26455a25e8..930c78ef89917f 100644 --- a/clang/test/CIR/CodeGen/matrix.cpp +++ b/clang/test/CIR/CodeGen/matrix.cpp @@ -158,3 +158,22 @@ void column_major_volatile_store() { // LLVM: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 // LLVM: %[[TMP_PTR:.*]] = load ptr, ptr %[[PTR_ADDR]], align 8 // LLVM: call void @llvm.matrix.column.major.store.v9f32.i64(<9 x float> %[[TMP_MATRIX]], ptr align 4 %[[TMP_PTR]], i64 3, i1 true, i32 3, i32 3) + +void matrix_subscript_expr() { + matrix3x3 matrix; + float b = matrix[1][2]; +} + +// CIR: %[[MATRIX_ADDR:.*]] = cir.alloca "matrix" {{.*}} : !cir.ptr<!cir.matrix<3 x 3 x !cir.float>> +// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.float> +// CIR: %[[TMP_MATRIX:.*]] = cir.load {{.*}} %[[MATRIX_ADDR]] : !cir.ptr<!cir.matrix<3 x 3 x !cir.float>>, !cir.matrix<3 x 3 x !cir.float> +// CIR: %[[ROW_IDX:.*]] = cir.const #cir.int<1> : !u64i +// CIR: %[[COLUMN_IDX:.*]] = cir.const #cir.int<2> : !u64i +// CIR: %[[ELEM:.*]] = cir.matrix.extract %[[TMP_MATRIX]][%[[ROW_IDX]] : !u64i, %[[COLUMN_IDX]] : !u64i] : !cir.matrix<3 x 3 x !cir.float> +// CIR: cir.store {{.*}} %[[ELEM]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float> + +// LLVM: %[[MATRIX_ADDR:.*]] = alloca [9 x float], align 4 +// LLVM: %[[B_ADDR:.*]] = alloca float, align 4 +// LLVM: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 +// LLVM: %[[ELEM:.*]] = extractelement <9 x float> %[[TMP_MATRIX]], i64 7 +// LLVM: store float %[[ELEM]], ptr %[[B_ADDR]], align 4 >From 01461631ebf77f930cbfb97bb92c02569dd71dc0 Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Thu, 8 Oct 2026 18:32:18 +0200 Subject: [PATCH 2/4] Address code review comments --- clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 +- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 6 --- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 7 ++- clang/lib/CIR/CodeGen/CIRGenFunction.h | 2 - .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 10 ++-- clang/test/CIR/CodeGen/matrix.cpp | 47 +++++++++++++++++-- 6 files changed, 54 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td index 36689745acd8fc..b3f00b38d507bc 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIROps.td +++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td @@ -6451,7 +6451,7 @@ def CIR_MatrixExtractOp : CIR_Op<"matrix.extract", [ let assemblyFormat = [{ $matrix - `[` $rowIdx `:` type($rowIdx) `,` $columnIdx `:` type($columnIdx) `]` + `[` $rowIdx `:` type($rowIdx) `]` `[` $columnIdx `:` type($columnIdx) `]` attr-dict `:` qualified(type($matrix)) }]; } diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 230edb0f9de06f..c03f7b355b6959 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -1552,12 +1552,6 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) { return lv; } -mlir::Value CIRGenFunction::emitMatrixIndexExpr(const Expr *e) { - mlir::Value idx = emitScalarExpr(e); - return idx.getType() == uIntPtrTy ? idx - : builder.createIntCast(idx, uIntPtrTy); -} - LValue CIRGenFunction::emitExtVectorElementExpr(const ExtVectorElementExpr *e) { // Emit the base vector as an l-value. LValue base; diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index a9cbdac30f0f8d..0854f6995ac138 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -320,10 +320,13 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> { } mlir::Value VisitMatrixSubscriptExpr(MatrixSubscriptExpr *e) { + mlir::Value rowIdx = cgf.emitScalarExpr(e->getRowIdx()); + mlir::Value columnIdx = cgf.emitScalarExpr(e->getColumnIdx()); mlir::Value matrix = Visit(e->getBase()); - mlir::Value rowIdx = cgf.emitMatrixIndexExpr(e->getRowIdx()); - mlir::Value columnIdx = cgf.emitMatrixIndexExpr(e->getColumnIdx()); mlir::Location loc = cgf.getLoc(e->getSourceRange()); + if (cgf.cgm.getCodeGenOpts().OptimizationLevel > 0) + cgf.cgm.errorNYI(e->getSourceRange(), + "VisitMatrixSubscriptExpr: emit index assumption"); return builder.createMatrixExtract(loc, matrix, rowIdx, columnIdx); } diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index 39bb3b7e8dea2e..6db6a232b9ece8 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -1854,8 +1854,6 @@ class CIRGenFunction : public CIRGenTypeCache { LValue emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e); LValue emitInitListLValue(const InitListExpr *e); - mlir::Value emitMatrixIndexExpr(const Expr *e); - LValue emitExtVectorElementExpr(const ExtVectorElementExpr *e); Address emitArrayToPointerDecay(const Expr *e, diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index f0d75eb6fced8c..af50ebd4d1c180 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -5275,16 +5275,18 @@ mlir::LogicalResult CIRToLLVMMatrixExtractOpLowering::matchAndRewrite( cir::MatrixExtractOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { cir::MatrixType matrixTy = op.getMatrix().getType(); - mlir::Value rowIdx = adaptor.getRowIdx(); - mlir::Value columnIdx = adaptor.getColumnIdx(); + mlir::Value rowIdx = createIntCast(rewriter, adaptor.getRowIdx(), + rewriter.getI64Type(), false); + mlir::Value columnIdx = createIntCast(rewriter, adaptor.getColumnIdx(), + rewriter.getI64Type(), false); mlir::Type indexTy = rowIdx.getType(); mlir::Value numRows = mlir::LLVM::ConstantOp::create( rewriter, op.getLoc(), indexTy, - rewriter.getI64IntegerAttr(matrixTy.getRowNum())); + rewriter.getIntegerAttr(indexTy, matrixTy.getRowNum())); // Column major index = (columnIdx * numRows) + rowIdx mlir::Value flatIndex = mlir::LLVM::MulOp::create( - rewriter, op->getLoc(), {columnIdx, numRows}, /*properties'clang=*/{}, + rewriter, op->getLoc(), {columnIdx, numRows}, /*properties=*/{}, /*discardableAttributes=*/{}); flatIndex = mlir::LLVM::AddOp::create(rewriter, op->getLoc(), flatIndex, rowIdx, /*overflowFlags=*/{}); diff --git a/clang/test/CIR/CodeGen/matrix.cpp b/clang/test/CIR/CodeGen/matrix.cpp index 930c78ef89917f..7a096b089b895c 100644 --- a/clang/test/CIR/CodeGen/matrix.cpp +++ b/clang/test/CIR/CodeGen/matrix.cpp @@ -1,9 +1,9 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fenable-matrix -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fenable-matrix -fclangir -emit-llvm %s -o %t-cir.ll -// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefixes=LLVM,LLVM-CIR // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fenable-matrix -emit-llvm %s -o %t.ll -// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM +// RUN: FileCheck --input-file=%t.ll %s -check-prefixes=LLVM,LLVM-OGCG typedef float matrix3x3 __attribute__((matrix_type(3, 3))); typedef float matrix3x2 __attribute__((matrix_type(3, 2))); @@ -166,10 +166,10 @@ void matrix_subscript_expr() { // CIR: %[[MATRIX_ADDR:.*]] = cir.alloca "matrix" {{.*}} : !cir.ptr<!cir.matrix<3 x 3 x !cir.float>> // CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.float> +// CIR: %[[ROW_IDX:.*]] = cir.const #cir.int<1> : !s32i +// CIR: %[[COLUMN_IDX:.*]] = cir.const #cir.int<2> : !s32i // CIR: %[[TMP_MATRIX:.*]] = cir.load {{.*}} %[[MATRIX_ADDR]] : !cir.ptr<!cir.matrix<3 x 3 x !cir.float>>, !cir.matrix<3 x 3 x !cir.float> -// CIR: %[[ROW_IDX:.*]] = cir.const #cir.int<1> : !u64i -// CIR: %[[COLUMN_IDX:.*]] = cir.const #cir.int<2> : !u64i -// CIR: %[[ELEM:.*]] = cir.matrix.extract %[[TMP_MATRIX]][%[[ROW_IDX]] : !u64i, %[[COLUMN_IDX]] : !u64i] : !cir.matrix<3 x 3 x !cir.float> +// CIR: %[[ELEM:.*]] = cir.matrix.extract %[[TMP_MATRIX]][%[[ROW_IDX]] : !s32i] [%[[COLUMN_IDX]] : !s32i] : !cir.matrix<3 x 3 x !cir.float> // CIR: cir.store {{.*}} %[[ELEM]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float> // LLVM: %[[MATRIX_ADDR:.*]] = alloca [9 x float], align 4 @@ -177,3 +177,40 @@ void matrix_subscript_expr() { // LLVM: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 // LLVM: %[[ELEM:.*]] = extractelement <9 x float> %[[TMP_MATRIX]], i64 7 // LLVM: store float %[[ELEM]], ptr %[[B_ADDR]], align 4 + +void matrix_subscript_expr_non_const_indices() { + matrix3x3 matrix; + unsigned long row; + unsigned long column; + float b = matrix[row][column]; +} + +// CIR: %[[MATRIX_ADDR:.*]] = cir.alloca "matrix" {{.*}} : !cir.ptr<!cir.matrix<3 x 3 x !cir.float>> +// CIR: %[[ROW_ADDR:.*]] = cir.alloca "row" {{.*}} : !cir.ptr<!u64i> +// CIR: %[[COLUMN_ADDR:.*]] = cir.alloca "column" {{.*}} : !cir.ptr<!u64i> +// CIR: %[[B_ADDR:.*]] = cir.alloca "b" {{.*}} init : !cir.ptr<!cir.float> +// CIR: %[[ROW_IDX:.*]] = cir.load {{.*}} %[[ROW_ADDR]] : !cir.ptr<!u64i>, !u64i +// CIR: %[[COLUMN_IDX:.*]] = cir.load {{.*}} %[[COLUMN_ADDR]] : !cir.ptr<!u64i>, !u64i +// CIR: %[[TMP_MATRIX:.*]] = cir.load {{.*}} %[[MATRIX_ADDR]] : !cir.ptr<!cir.matrix<3 x 3 x !cir.float>>, !cir.matrix<3 x 3 x !cir.float> +// CIR: %[[ELEM:.*]] = cir.matrix.extract %[[TMP_MATRIX]][%[[ROW_IDX]] : !u64i] [%[[COLUMN_IDX]] : !u64i] : !cir.matrix<3 x 3 x !cir.float> +// CIR: cir.store {{.*}} %[[ELEM]], %[[B_ADDR]] : !cir.float, !cir.ptr<!cir.float> + +// LLVM: %[[MATRIX_ADDR:.*]] = alloca [9 x float], align 4 +// LLVM: %[[ROW_ADDR:.*]] = alloca i64, align 8 +// LLVM: %[[COLUMN_ADDR:.*]] = alloca i64, align 8 +// LLVM: %[[B_ADDR:.*]] = alloca float, align 4 +// LLVM: %[[ROW_IDX:.*]] = load i64, ptr %[[ROW_ADDR]], align 8 +// LLVM: %[[COLUMN_IDX:.*]] = load i64, ptr %[[COLUMN_ADDR]], align 8 + +// Difference here because CIR calculate the flat index in lowering pass after matrix is loaded + +// LLVM-CIR: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 +// LLVM-CIR: %[[MUL_COL_3:.*]] = mul i64 %[[COLUMN_IDX]], 3 +// LLVM-CIR: %[[FLAT_IDX:.*]] = add i64 %[[MUL_COL_3]], %[[ROW_IDX]] + +// LLVM-OGCG: %[[MUL_COL_3:.*]] = mul i64 %[[COLUMN_IDX]], 3 +// LLVM-OGCG: %[[FLAT_IDX:.*]] = add i64 %[[MUL_COL_3]], %[[ROW_IDX]] +// LLVM-OGCG: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 + +// LLVM: %[[ELEM:.*]] = extractelement <9 x float> %[[TMP_MATRIX]], i64 %[[FLAT_IDX]] +// LLVM: store float %[[ELEM]], ptr %[[B_ADDR]], align 4 >From fbf25d84fb4fea1d7a9710d3410a3ce2553ae0e4 Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Fri, 9 Oct 2026 17:40:23 +0200 Subject: [PATCH 3/4] Address code review comments --- clang/include/clang/CIR/MissingFeatures.h | 1 + clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 3 +-- clang/test/CIR/CodeGen/matrix.cpp | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h index 68e15b84cea165..01d85456df6f56 100644 --- a/clang/include/clang/CIR/MissingFeatures.h +++ b/clang/include/clang/CIR/MissingFeatures.h @@ -235,6 +235,7 @@ struct MissingFeatures { static bool emitLValueAlignmentAssumption() { return false; } static bool emitNullCheckForDeleteCalls() { return false; } static bool emitNullabilityCheck() { return false; } + static bool emitIndexAssumption() { return false; } static bool emitTypeCheck() { return false; } static bool emitTypeMetadataCodeForVCall() { return false; } static bool fakeUseCleanup() { return false; } diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 0854f6995ac138..7a07ebc1ad19df 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -325,8 +325,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> { mlir::Value matrix = Visit(e->getBase()); mlir::Location loc = cgf.getLoc(e->getSourceRange()); if (cgf.cgm.getCodeGenOpts().OptimizationLevel > 0) - cgf.cgm.errorNYI(e->getSourceRange(), - "VisitMatrixSubscriptExpr: emit index assumption"); + assert(!cir::MissingFeatures::emitIndexAssumption()); return builder.createMatrixExtract(loc, matrix, rowIdx, columnIdx); } diff --git a/clang/test/CIR/CodeGen/matrix.cpp b/clang/test/CIR/CodeGen/matrix.cpp index 7a096b089b895c..19d87a8829aab1 100644 --- a/clang/test/CIR/CodeGen/matrix.cpp +++ b/clang/test/CIR/CodeGen/matrix.cpp @@ -205,11 +205,10 @@ void matrix_subscript_expr_non_const_indices() { // Difference here because CIR calculate the flat index in lowering pass after matrix is loaded // LLVM-CIR: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 -// LLVM-CIR: %[[MUL_COL_3:.*]] = mul i64 %[[COLUMN_IDX]], 3 -// LLVM-CIR: %[[FLAT_IDX:.*]] = add i64 %[[MUL_COL_3]], %[[ROW_IDX]] -// LLVM-OGCG: %[[MUL_COL_3:.*]] = mul i64 %[[COLUMN_IDX]], 3 -// LLVM-OGCG: %[[FLAT_IDX:.*]] = add i64 %[[MUL_COL_3]], %[[ROW_IDX]] +// LLVM: %[[MUL_COL_3:.*]] = mul i64 %[[COLUMN_IDX]], 3 +// LLVM: %[[FLAT_IDX:.*]] = add i64 %[[MUL_COL_3]], %[[ROW_IDX]] + // LLVM-OGCG: %[[TMP_MATRIX:.*]] = load <9 x float>, ptr %[[MATRIX_ADDR]], align 4 // LLVM: %[[ELEM:.*]] = extractelement <9 x float> %[[TMP_MATRIX]], i64 %[[FLAT_IDX]] >From 4711545b379d6efde995c5cf9b351a07ce8454fd Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Fri, 9 Oct 2026 17:47:34 +0200 Subject: [PATCH 4/4] Update missing feature name --- clang/include/clang/CIR/MissingFeatures.h | 2 +- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h index 01d85456df6f56..cd03e0a765cd94 100644 --- a/clang/include/clang/CIR/MissingFeatures.h +++ b/clang/include/clang/CIR/MissingFeatures.h @@ -233,9 +233,9 @@ struct MissingFeatures { static bool emitLifetimeMarkers() { return false; } static bool lifetimeMarkersBypass() { return false; } static bool emitLValueAlignmentAssumption() { return false; } + static bool emitMatrixIndexAssumption() { return false; } static bool emitNullCheckForDeleteCalls() { return false; } static bool emitNullabilityCheck() { return false; } - static bool emitIndexAssumption() { return false; } static bool emitTypeCheck() { return false; } static bool emitTypeMetadataCodeForVCall() { return false; } static bool fakeUseCleanup() { return false; } diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 7a07ebc1ad19df..1412ef103272d1 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -325,7 +325,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> { mlir::Value matrix = Visit(e->getBase()); mlir::Location loc = cgf.getLoc(e->getSourceRange()); if (cgf.cgm.getCodeGenOpts().OptimizationLevel > 0) - assert(!cir::MissingFeatures::emitIndexAssumption()); + assert(!cir::MissingFeatures::emitMatrixIndexAssumption()); return builder.createMatrixExtract(loc, matrix, rowIdx, columnIdx); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
