This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][VP] Support vp.reduce.mul by ExpandVectorPredication

Authored by inclyc on Dec 9 2022, 8:59 AM.



Most of VP intrinsics are implemented in RISC-V backends, but vp.reduce.mul (element length > 1) does not yet. Legalizes vp.reduce.mul using ExpandVectorPredication Pass.

Diff Detail

Event Timeline

inclyc created this revision.Dec 9 2022, 8:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 9 2022, 8:59 AM
inclyc published this revision for review.Dec 9 2022, 9:00 AM

Does this work for scalable vectors?

Does this work for scalable vectors?

I'm afraid not.... expand vp pass seems not able to expand reduction (causes an assertion failure).


declare i8 @llvm.vp.reduce.mul.nxv2i8(i8, <vscale x 2 x i8>, <vscale x 2 x i1>, i32)

define signext i8 @vpreduce_mul_nxv2i8(i8 signext %s, <vscale x 2 x i8> %v, <vscale x 2 x i1> %m, i32 zeroext %evl) {
  %r = call i8 @llvm.vp.reduce.mul.nxv2i8(i8 %s, <vscale x 2 x i8> %v, <vscale x 2 x i1> %m, i32 %evl)
  ret i8 %r

After expand vp:

define signext i8 @vpreduce_mul_nxv2i8(i8 signext %s, <vscale x 2 x i8> %v, <vscale x 2 x i1> %m, i32 zeroext %evl) {
  %1 = call <vscale x 2 x i1> 0, i32 %evl)
  %2 = and <vscale x 2 x i1> %1, %m
  %vscale = call i32 @llvm.vscale.i32()
  %scalable_size = mul nuw i32 %vscale, 2
  %3 = select <vscale x 2 x i1> %2, <vscale x 2 x i8> %v, <vscale x 2 x i8> shufflevector (<vscale x 2 x i8> insertelement (<vscale x 2 x i8> poison, i8 1, i32 0), <vscale x 2 x i8> poison, <vscale x 2 x i32> zeroinitializer)
  %4 = call i8 @llvm.vector.reduce.mul.nxv2i8(<vscale x 2 x i8> %3)
  %5 = mul i8 %4, %s
  ret i8 %5

Other targets may not able to handle this as well.

Perhaps some of our next patches should be to improve the related passes (like Expand reduction intrinsics)?

Stack dump:

llc: <llvm-project>/llvm/include/llvm/Support/Casting.h:578: decltype(auto) llvm::cast(From *) [To = llvm::FixedVectorType, From = llvm::Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
PLEASE submit a bug report to and include the crash backtrace.
Stack dump:
0.      Program arguments: build/bin/llc -mtriple=riscv32 local/buddy/reduce/mul/scale.ll
1.      Running pass 'Function Pass Manager' on module 'local/buddy/reduce/mul/scale.ll'.
2.      Running pass 'Expand reduction intrinsics' on function '@vpreduce_mul_nxv2i8'
 #0 0x00007f5ebd0c31da llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) <llvm-project>/llvm/lib/Support/Unix/
 #1 0x00007f5ebd0c338b PrintStackTraceSignalHandler(void*) <llvm-project>/llvm/lib/Support/Unix/
 #2 0x00007f5ebd0c1a16 llvm::sys::RunSignalHandlers() <llvm-project>/llvm/lib/Support/Signals.cpp:104:5
 #3 0x00007f5ebd0c3a75 SignalHandler(int) <llvm-project>/llvm/lib/Support/Unix/
 #4 0x00007f5ebc7c2950 (/usr/lib64/
 #5 0x00007f5ebc80e42c (/usr/lib64/
 #6 0x00007f5ebc7c28b2 raise (/usr/lib64/
 #7 0x00007f5ebc7ad471 abort (/usr/lib64/
 #8 0x00007f5ebc7ad395 (/usr/lib64/
 #9 0x00007f5ebc7bbb92 (/usr/lib64/
#10 0x00007f5ec07c8c88 decltype(auto) llvm::cast<llvm::FixedVectorType, llvm::Type>(llvm::Type*) <llvm-project>/llvm/include/llvm/Support/Casting.h:579:10
#11 0x00007f5ec080c6d6 (anonymous namespace)::expandReductions(llvm::Function&, llvm::TargetTransformInfo const*) <llvm-project>/llvm/lib/CodeGen/ExpandReductions.cpp:147:15
#12 0x00007f5ec080c976 (anonymous namespace)::ExpandReductions::runOnFunction(llvm::Function&) <llvm-project>/llvm/lib/CodeGen/ExpandReductions.cpp:183:5
#13 0x00007f5ebdcb9643 llvm::FPPassManager::runOnFunction(llvm::Function&) <llvm-project>/llvm/lib/IR/LegacyPassManager.cpp:1430:23
#14 0x00007f5ebdcbe462 llvm::FPPassManager::runOnModule(llvm::Module&) <llvm-project>/llvm/lib/IR/LegacyPassManager.cpp:1476:16
#15 0x00007f5ebdcb9f19 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) <llvm-project>/llvm/lib/IR/LegacyPassManager.cpp:1545:23
#16 0x00007f5ebdcb9a8a llvm::legacy::PassManagerImpl::run(llvm::Module&) <llvm-project>/llvm/lib/IR/LegacyPassManager.cpp:535:16
#17 0x00007f5ebdcbe741 llvm::legacy::PassManager::run(llvm::Module&) <llvm-project>/llvm/lib/IR/LegacyPassManager.cpp:1672:3
#18 0x000055f10326689a compileModule(char**, llvm::LLVMContext&) <llvm-project>/llvm/tools/llc/llc.cpp:737:41
#19 0x000055f103264ca2 main <llvm-project>/llvm/tools/llc/llc.cpp:418:13
#20 0x00007f5ebc7ae34a (/usr/lib64/
#21 0x00007f5ebc7ae3fc __libc_start_main (/usr/lib64/
#22 0x000055f1032644b1 _start (build/bin/llc+0x284b1)
zsh: IOT instruction (core dumped)  build/bin/llc -mtriple=riscv32 local/buddy/reduce/mul/scale.ll
craig.topper added inline comments.Dec 13 2022, 1:17 PM
1010 ↗(On Diff #481667)

This test file has "mask" in its name because it is the test file for i1 vectors. These tests should be be in fixed-vectors-reduction-vp.ll

inclyc updated this revision to Diff 482718.Dec 13 2022, 10:19 PM

Hoist tests to fixed-vectors-reduction-int-vp.ll.

reames accepted this revision.Dec 16 2022, 12:51 PM
reames added a subscriber: reames.


This revision is now accepted and ready to land.Dec 16 2022, 12:51 PM
This revision was landed with ongoing or failed builds.Dec 16 2022, 6:50 PM
This revision was automatically updated to reflect the committed changes.