Skip to content

Commit 76decd1

Browse files
Don't use GCD to calculate EMUL
1 parent a3ef167 commit 76decd1

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

llvm/lib/Target/RISCV/MCA/RISCVCustomBehaviour.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include "TargetInfo/RISCVTargetInfo.h"
1818
#include "llvm/MC/TargetRegistry.h"
1919
#include "llvm/Support/Debug.h"
20-
#include <numeric>
21-
#include <set>
2220

2321
#define DEBUG_TYPE "llvm-mca-riscv-custombehaviour"
2422

@@ -188,18 +186,29 @@ RISCVInstrumentManager::createInstruments(const MCInst &Inst) {
188186
}
189187

190188
/// Return EMUL = (EEW / SEW) * LMUL
191-
inline static std::pair<unsigned, bool>
189+
inline static RISCVII::VLMUL
192190
getEMULEqualsEEWDivSEWTimesLMUL(unsigned EEW, unsigned SEW,
193191
RISCVII::VLMUL VLMUL) {
194-
// Calculate (EEW/SEW)*LMUL preserving fractions less than 1. Use GCD
195-
// to put fraction in simplest form.
196-
auto [LMUL, Fractional] = RISCVVType::decodeVLMUL(VLMUL);
197-
unsigned Num = EEW, Denom = SEW;
198-
int GCD =
199-
Fractional ? std::gcd(Num, Denom * LMUL) : std::gcd(Num * LMUL, Denom);
200-
Num = Fractional ? Num / GCD : Num * LMUL / GCD;
201-
Denom = Fractional ? Denom * LMUL / GCD : Denom / GCD;
202-
return std::make_pair(Num > Denom ? Num : Denom, Denom > Num);
192+
bool IsScaleFrac = EEW < SEW;
193+
unsigned Scale = IsScaleFrac ? SEW / EEW : EEW / SEW;
194+
auto [LMUL, IsLMULFrac] = RISCVVType::decodeVLMUL(VLMUL);
195+
196+
unsigned EMUL;
197+
bool EMULFrac;
198+
if ((IsScaleFrac && IsLMULFrac) || (!IsScaleFrac && !IsLMULFrac)) {
199+
EMUL = LMUL * Scale;
200+
EMULFrac = IsLMULFrac;
201+
} else if (Scale > LMUL) {
202+
EMUL = Scale / LMUL;
203+
EMULFrac = IsScaleFrac;
204+
} else {
205+
EMUL = LMUL / Scale;
206+
EMULFrac = IsLMULFrac;
207+
}
208+
if (EMUL == 1)
209+
EMULFrac = false;
210+
211+
return RISCVVType::encodeLMUL(EMUL, EMULFrac);
203212
}
204213

205214
static std::pair<uint8_t, uint8_t>
@@ -230,13 +239,8 @@ getEEWAndEMULForUnitStrideLoadStore(unsigned Opcode, uint8_t LMUL,
230239
}
231240

232241
RISCVII::VLMUL VLMUL = static_cast<RISCVII::VLMUL>(LMUL);
233-
auto [EMULPart, Fractional] =
234-
getEMULEqualsEEWDivSEWTimesLMUL(EEW, SEW, VLMUL);
235-
assert(RISCVVType::isValidLMUL(EMULPart, Fractional) &&
236-
"Unexpected EEW from instruction used with LMUL and SEW");
237-
238-
uint8_t EMUL =
239-
static_cast<RISCVII::VLMUL>(RISCVVType::encodeLMUL(EMULPart, Fractional));
242+
uint8_t EMUL = static_cast<RISCVII::VLMUL>(
243+
getEMULEqualsEEWDivSEWTimesLMUL(EEW, SEW, VLMUL));
240244
return std::make_pair(EEW, EMUL);
241245
}
242246

0 commit comments

Comments
 (0)