diff --git a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp --- a/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp +++ b/llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp @@ -1776,9 +1776,7 @@ else goto MatchFail; - unsigned LmulLog2 = Log2_32(Lmul); - RISCVII::VLMUL VLMUL = - static_cast(Fractional ? 8 - LmulLog2 : LmulLog2); + RISCVII::VLMUL VLMUL = RISCVVType::encodeLMUL(Lmul, Fractional); unsigned VTypeI = RISCVVType::encodeVTYPE(VLMUL, Sew, TailAgnostic, MaskAgnostic); diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h @@ -383,6 +383,12 @@ // Decode VLMUL into 1,2,4,8 and fractional indicator. std::pair decodeVLMUL(RISCVII::VLMUL VLMUL); +inline static RISCVII::VLMUL encodeLMUL(unsigned LMUL, bool Fractional) { + assert(isValidLMUL(LMUL, Fractional) && "Unsupported LMUL"); + unsigned LmulLog2 = Log2_32(LMUL); + return static_cast(Fractional ? 8 - LmulLog2 : LmulLog2); +} + inline static unsigned decodeVSEW(unsigned VSEW) { assert(VSEW < 8 && "Unexpected VSEW value"); return 1 << (VSEW + 3);