Skip to content

[Codegen][CPU] Honor --iree-llvmcpu-skip-intermediate-roundings=false for RVV Zvfhmin f16 inner_tiled MMA - #25125

Merged
AGindinson merged 1 commit into
iree-org:mainfrom
liuyalong:users/yalong/honor-skip-intermediate-roundings-rvv-zvfhmin
Nov 18, 2026
Merged

AGindinson merged 1 commit into
iree-org:mainfrom
liuyalong:users/yalong/honor-skip-intermediate-roundings-rvv-zvfhmin

Conversation

@liuyalong

@liuyalong liuyalong commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Zvfhmin lacks native f16 MAC, so f16 MMA widens accumulator to f32
then truncates back. These extf/truncf pairs were marked as invertible
and hoisted out of K loop, dropping per-K rounding and producing wrong
answers. The skip-intermediate-roundings flag was not plumbed into
lowering at all.

Propagate skipIntermediateRoundings through inner tile lowering and the
LLVMCPU vector pipelines. When we need to preserve roundings (widenF16Acc
&& !skipIntermediateRoundings), materialize the f16 -> f32 -> f16
round-trip directly around each MA step in emitIntrinsic, and drop the
hoistable distribute/reassemble accumulator pair.

Add tests covering both keep-rounding and skip-rounding modes.

Fixes: #25058
Assisted-by: Claude Code, Doubao, DeepSeek

@liuyalong

Copy link
Copy Markdown
Contributor Author
  1. When skip-intermediate-roundings=false, keeping the per-K truncf inside the for loop:
iree$ iree-opt --pass-pipeline="builtin.module(func.func(iree-llvmcpu-virtual-vector-lowering{skip-intermediate-roundings=false}))"   --split-input-file compiler/src/iree/compiler/Codegen/LLVMCPU/test/virtual_vector_lowering.mlir  | awk '/func.func @widenf32_kloop/,/^}/'
compiler/src/iree/compiler/Codegen/LLVMCPU/test/virtual_vector_lowering.mlir:179:10: remark: hoistable_conversion was not hoisted or cancelled; inlining in place data_tiled_acc_distribute inverting data_tiled_acc_reassemble
    %0 = iree_codegen.inner_tiled ins(%lhs, %rhs) outs(%acc) <
         ^
compiler/src/iree/compiler/Codegen/LLVMCPU/test/virtual_vector_lowering.mlir:179:10: note: see current operation: 
%4 = util.hoistable_conversion "data_tiled_acc_distribute" inverts("data_tiled_acc_reassemble") (%arg6 = %arg5) : (vector<1x32xf16>) -> vector<[8]xf32> {
  %11 = vector.shape_cast %arg6 : vector<1x32xf16> to vector<32xf16>
  %12 = arith.extf %11 : vector<32xf16> to vector<32xf32>
  %13 = vector.scalable.insert %12, %0[0] : vector<32xf32> into vector<[8]xf32>
  util.return %13 : vector<[8]xf32>
} {iree.lossy_roundtrip}
  func.func @widenf32_kloop(%arg0: vector<1x1xf16>, %arg1: vector<32x1xf16>, %arg2: vector<1x32xf16>, %arg3: index) -> vector<1x32xf16> {
    %c1_i64 = arith.constant 1 : i64
    %c32_i64 = arith.constant 32 : i64
    %c7_i64 = arith.constant 7 : i64
    %0 = ub.poison : vector<[8]xf32>
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %1 = scf.for** %arg4 = %c0 to %arg3 step %c1 iter_args(%arg5 = %arg2) -> (vector<1x32xf16>) { // for loop start
      %2 = vector.shape_cast %arg0 : vector<1x1xf16> to vector<1xf16>
      %3 = vector.shape_cast %arg1 : vector<32x1xf16> to vector<32xf16>
      %4 = vector.shape_cast %arg5 : vector<1x32xf16> to vector<32xf16>
      %5 = arith.extf %4 : vector<32xf16> to vector<32xf32>                                       // extf inside for loop
      %6 = vector.scalable.insert %5, %0[0] : vector<32xf32> into vector<[8]xf32>
      %7 = arith.extf %2 : vector<1xf16> to vector<1xf32>
      %8 = arith.extf %3 : vector<32xf16> to vector<32xf32>
      %9 = vector.extract %7[0] : f32 from vector<1xf32>
      %10 = vector.scalable.insert %8, %0[0] : vector<32xf32> into vector<[8]xf32>
      %11 = llvm.call_intrinsic "llvm.riscv.vfmacc"(%6, %9, %10, %c7_i64, %c32_i64, %c1_i64) : (vector<[8]xf32>, f32, vector<[8]xf32>, i64, i64, i64) -> vector<[8]xf32>
      %12 = vector.scalable.extract %11[0] : vector<32xf32> from vector<[8]xf32>
      %13 = arith.truncf %12 : vector<32xf32> to vector<32xf16>                                 // truncf inside for loop
      %14 = vector.shape_cast %13 : vector<32xf16> to vector<1x32xf16>
      scf.yield %14 : vector<1x32xf16>
    }                                                                                                                                   // for loop end
    return %1 : vector<1x32xf16>
  }
}

  1. When skip-intermediate-roundings=true, extf/truncf is marked as pairs and hoisted:
iree$ iree-opt --pass-pipeline="builtin.module(func.func(iree-llvmcpu-virtual-vector-lowering{skip-intermediate-roundings=true}))"   --split-input-file compiler/src/iree/compiler/Codegen/LLVMCPU/test/virtual_vector_lowering.mlir  | awk '/func.func @widenf32_kloop/,/^}/'
  func.func @widenf32_kloop(%arg0: vector<1x1xf16>, %arg1: vector<32x1xf16>, %arg2: vector<1x32xf16>, %arg3: index) -> vector<1x32xf16> {
    %0 = ub.poison : vector<1x32xf16>
    %c1_i64 = arith.constant 1 : i64
    %c32_i64 = arith.constant 32 : i64
    %c7_i64 = arith.constant 7 : i64
    %1 = ub.poison : vector<[8]xf32>
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %2 = vector.shape_cast %arg2 : vector<1x32xf16> to vector<32xf16>
    %3 = arith.extf %2 : vector<32xf16> to vector<32xf32>                                                                   //extf outside for loop
    %4 = vector.scalable.insert %3, %1[0] : vector<32xf32> into vector<[8]xf32>
    %5:2 = scf.for %arg4 = %c0 to %arg3 step %c1 iter_args(%arg5 = %0, %arg6 = %4) -> (vector<1x32xf16>, vector<[8]xf32>) {                                                                                                                                   // for loop start
      %9 = vector.shape_cast %arg0 : vector<1x1xf16> to vector<1xf16>
      %10 = vector.shape_cast %arg1 : vector<32x1xf16> to vector<32xf16>
      %11 = arith.extf %9 : vector<1xf16> to vector<1xf32>
      %12 = arith.extf %10 : vector<32xf16> to vector<32xf32>
      %13 = vector.extract %11[0] : f32 from vector<1xf32>
      %14 = vector.scalable.insert %12, %1[0] : vector<32xf32> into vector<[8]xf32>
      %15 = llvm.call_intrinsic "llvm.riscv.vfmacc"(%arg6, %13, %14, %c7_i64, %c32_i64, %c1_i64) : (vector<[8]xf32>, f32, vector<[8]xf32>, i64, i64, i64) -> vector<[8]xf32>
      scf.yield %arg5, %15 : vector<1x32xf16>, vector<[8]xf32>
    }                                                                                                                                                           // for loop end
    %6 = vector.scalable.extract %5#1[0] : vector<32xf32> from vector<[8]xf32>
    %7 = arith.truncf %6 : vector<32xf32> to vector<32xf16>                                                               // truncf outside for loop
    %8 = vector.shape_cast %7 : vector<32xf16> to vector<1x32xf16>
    return %8 : vector<1x32xf16>
  }
}

@liuyalong

Copy link
Copy Markdown
Contributor Author

@Manewing This resolves issue #25058 (reported by @TMahlatini), addressing your feedback from #25000 (comment). All checks have passed, could you take a look?

@AGindinson

Copy link
Copy Markdown
Contributor

@liuyalong I will take a look at the PR shortly. Sorry for the delayed review.

@AGindinson AGindinson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fix! I understand the idea with the lossy_roundtrip attribute, but to my mind this overcomplicates things and forces too much plumbing for a conceptually simple change.

@liuyalong

liuyalong commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

For new version, the command still works, it just produces the new materialized form now, not the old iree.lossy_roundtrip form. I can use the command to capture both IR and diagnostics and stop at the function boundary(when skip-intermediate-roundings=false):

/mnt/Disk_2/iree/iree-build/tools/iree-opt \
  --pass-pipeline="builtin.module(func.func(iree-llvmcpu-virtual-vector-lowering{skip-intermediate-roundings=false}))" \
  --split-input-file compiler/src/iree/compiler/Codegen/LLVMCPU/test/virtual_vector_lowering.mlir 2>&1 \
  | awk '/func\.func @widenf32_kloop/,/^  }$/'

Current output shows the same key property, but with the distributed f16 accumulator carried explicitly:

  %3:2 = scf.for ... iter_args(%arg5 = %0, %arg6 = %2)
    -> (vector<1x32xf16>, vector<32xf16>) {
  %7 = arith.extf %arg6 : vector<32xf16> to vector<32xf32>
  ...
  %13 = llvm.call_intrinsic "llvm.riscv.vfmacc"
  %14 = vector.scalable.extract %13[0] : vector<32xf32>
  %15 = arith.truncf %14 : vector<32xf32> to vector<32xf16>
  scf.yield %arg5, %15 : vector<1x32xf16>, vector<32xf16>
}

So extf/truncf are still inside the K loop. What is gone is {iree.lossy_roundtrip} and the “not hoisted or cancelled” remark, because the rounding is now materialized in CPU lowering instead of being communicated through IR.

@liuyalong

Copy link
Copy Markdown
Contributor Author

skip-intermediate-roundings behavior

This option controls whether the f16 accumulator must be rounded on every K
iteration, or may stay in f32 across the whole K loop.

  • skip-intermediate-roundings=true (default)
    The compiler is allowed to keep the accumulator in f32 across the K loop
    and apply the f16 rounding only once at the end. Hoisting extf out before
    the loop and truncf after it is exactly what this option enables.

  • skip-intermediate-roundings=false
    Preserve the per-K f16 rounding. This is the case we materialize inside
    the loop (wrapping each intrinsic with explicit legalizeAcc()/restoreAcc()).

The two outputs therefore represent two valid modes:

flag accumulation extf/truncf semantics
true f32 across K hoisted out of loop skips intermediate f16 rounding
false f16 each iteration inside loop strict per-K f16 rounding

Keeping the accumulator in f32 is usually more numerically precise, but it
does not match strict f16 round-per-iteration semantics — which is why the
option exists. The hoisted form is not a bug when true.

@liuyalong
liuyalong force-pushed the users/yalong/honor-skip-intermediate-roundings-rvv-zvfhmin branch from 6241ae8 to b7a25e0 Compare November 2, 2026 05:51
@AGindinson
AGindinson self-requested a review November 2, 2026 14:30
@liuyalong
liuyalong force-pushed the users/yalong/honor-skip-intermediate-roundings-rvv-zvfhmin branch from b7a25e0 to e9e7b4a Compare November 3, 2026 02:26
@liuyalong

Copy link
Copy Markdown
Contributor Author

@AGindinson
Thanks for kick off the CI. The windows_x64_msvc failure is in tile_and_fuse_producer_consumer_anchoring_root_op.mlir (a tiling/packing test), which is unrelated to this PR. Other platforms passed. It looks like a flaky error, it's a thread unsafe case from the error log:

# .---command stderr------------
# | C:\home\runner\_work\iree\iree\compiler\src\iree\compiler\Codegen\LLVMCPU\test\tile_and_fuse_producer_consumer_anchoring_root_op.mlir:254:8: warning: tiling is not thread safe at axis #2
# |   %1 = linalg.matmul {lowering_config = #config}

I rebase the code onto origin:main, could you please re-kick off it?

@liuyalong

Copy link
Copy Markdown
Contributor Author

@AGindinson Thanks for kick off the CI. The windows_x64_msvc failure is in tile_and_fuse_producer_consumer_anchoring_root_op.mlir (a tiling/packing test), which is unrelated to this PR. Other platforms passed. It looks like a flaky error, it's a thread unsafe case from the error log:

# .---command stderr------------
# | C:\home\runner\_work\iree\iree\compiler\src\iree\compiler\Codegen\LLVMCPU\test\tile_and_fuse_producer_consumer_anchoring_root_op.mlir:254:8: warning: tiling is not thread safe at axis #2
# |   %1 = linalg.matmul {lowering_config = #config}

I rebase the code onto origin:main, could you please re-kick off it?

@AGindinson Thanks for kicking off the CI again. All the latest CI results have passed.

@AGindinson AGindinson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry for another delay @liuyalong. Generally LGTM with a couple suggestions

@liuyalong

Copy link
Copy Markdown
Contributor Author

@AGindinson Thanks for kick off the newest CI. The linux_x64_clang_ubsan failure is a timeout (build and test timeout). All others has passed.

… for RVV Zvfhmin f16 inner_tiled MMA

Zvfhmin lacks native f16 MAC, so f16 MMA widens accumulator to f32
then truncates back. These extf/truncf pairs were marked as invertible
and hoisted out of K loop, dropping per-K rounding and producing wrong
answers. The skip-intermediate-roundings flag was not plumbed into
lowering at all.

Propagate skipIntermediateRoundings through inner tile lowering and the
LLVMCPU vector pipelines. When we need to preserve roundings (widenF16Acc
&& !skipIntermediateRoundings), materialize the f16 -> f32 -> f16
round-trip directly around each MA step in emitIntrinsic, and drop the
hoistable distribute/reassemble accumulator pair.

Add tests covering both keep-rounding and skip-rounding modes.

Fixes: iree-org#25058
Assisted-by: Claude Code, Doubao, DeepSeek
Signed-off-by: Yalong <[email protected]>
@liuyalong
liuyalong force-pushed the users/yalong/honor-skip-intermediate-roundings-rvv-zvfhmin branch from 5464c8c to e9e7b4a Compare November 17, 2026 14:01
@liuyalong
liuyalong force-pushed the users/yalong/honor-skip-intermediate-roundings-rvv-zvfhmin branch from e9e7b4a to ea38807 Compare November 17, 2026 14:19

@AGindinson AGindinson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@AGindinson
AGindinson merged commit 8071457 into iree-org:main Nov 18, 2026
82 of 84 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Codegen][CPU] Honor --iree-llvmcpu-skip-intermediate-roundings=false for RVV Zvfhmin f16 inner_tiled MMA

2 participants