Skip to content

Fix Gemmini convolution instruction encoding to match hardware spec#689

Open
ashvin-verma wants to merge 1 commit intobuddy-compiler:mainfrom
ashvin-verma:gemmini_conv_patch
Open

Fix Gemmini convolution instruction encoding to match hardware spec#689
ashvin-verma wants to merge 1 commit intobuddy-compiler:mainfrom
ashvin-verma:gemmini_conv_patch

Conversation

@ashvin-verma
Copy link

Summary

  • Fixed incorrect bit-packing in loop_conv_ws_config* instructions in LegalizeForLLVMExport.cpp that caused convolution operations to produce wrong results (matmul operations were unaffected)
  • The primary bug was in CONFIG_4 where stride fields (inStride, weightStride, outStride) were completely missing from rs2, and kernelDilation was misplaced
  • Also corrected bit shifts in CONFIG_1, CONFIG_2, and CONFIG_3, and added missing parameters (inColDim, outColDim, poolOutColDim) to the gemminiLoopConvWs function signature

Changes to LegalizeForLLVMExport.cpp

Config Field Bug Fix
CONFIG_1 rs2 padding, stride, outColDim padding at bit 48, stride at 32, outColDim missing padding at bit 56, stride at 48, outColDim at 32
CONFIG_2 rs1 poolOutColDim, poolSize, poolStride, poolPadding poolOutColDim missing, poolSize at 32, poolStride at 16, poolPadding at 0 poolOutColDim at 32, poolSize at 16, poolStride at 8, poolPadding at 0
CONFIG_3 rs2 dpad, plpad, inColDim dpad at bit 16, plpad at 0, inColDim missing dpad at 24, plpad at 16, inColDim at 0
CONFIG_4 kernelDilation, strides kernelDilation in rs2 at 16, strides missing entirely kernelDilation in rs1 at 0, inStride/weightStride/outStride in rs2

Reference

Encoding corrected to match the Gemmini hardware specification as defined in:

Validation

Test Expected Before Fix After Fix
conv (17x17, k=3, stride=2) 950 -478 950
conv_with_pool 30827 0 30827

Tested on Spike with Gemmini extension against Gemmini C reference implementation outputs.

Test plan

  • Conv unit test output matches Gemmini C baseline
  • Conv with pooling output matches Gemmini C baseline
  • Upstream CI

The loop_conv_ws config instructions had incorrect bit-packing that
caused convolution operations to produce wrong results (matmuls worked).

Changes:
- CONFIG_1: Fix rs2 shifts and add outColDim field
- CONFIG_2: Fix rs1 to include poolOutColDim and correct shifts
- CONFIG_3: Fix rs2 shifts for dpad/plpad and add inColDim field
- CONFIG_4: Move kernelDilation to rs1, add stride fields to rs2
- Add missing parameters to gemminiLoopConvWs function signature
- Update call site to pass inColDim, outColDim, poolOutColDim, and
  stride parameters

Reference: gemmini.h gemmini_loop_conv_ws macro
@shirohasuki
Copy link
Contributor

Thanks for your time! I also found these mistakes, and fixed in this PR(#635).

ashvin-verma added a commit to ucb-bar/merlin that referenced this pull request Feb 9, 2026
Add reproducible benchmarks comparing Buddy-MLIR's Gemmini dialect
backend against the Gemmini C reference on Spike simulator.

Kernel benchmarks: conv, conv+pool, MLP2 (WS/OS), MLP1, softmax
matmul, iGELU matmul. ResNet50 conv1 layer validation with
intentional bad case for test methodology verification.

Conv benchmarks require buddy-compiler/buddy-mlir#689 (conv encoding
fix) for correct im2col lowering.
@linuxlonelyeagle
Copy link
Member

@ashvin-verma @shirohasuki I’m really glad to see that you are contributing to Gemmini support in buddy-mlir.

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.

3 participants