Files
TheRock/compiler/amd-llvm/llvm/lib/Target/Mips/MipsMulMulBugPass.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

133 lines
3.7 KiB
C++

//===- MipsMulMulBugPass.cpp - Mips VR4300 mulmul bugfix pass -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Early revisions of the VR4300 have a hardware bug where two consecutive
// multiplications can produce an incorrect result in the second multiply.
//
// This pass scans for mul instructions in each basic block and inserts
// a nop whenever the following conditions are met:
//
// - The current instruction is a single or double-precision floating-point
// mul instruction.
// - The next instruction is either a mul instruction (any kind)
// or a branch instruction.
//===----------------------------------------------------------------------===//
#include "Mips.h"
#include "MipsInstrInfo.h"
#include "MipsSubtarget.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetMachine.h"
#define DEBUG_TYPE "mips-vr4300-mulmul-fix"
using namespace llvm;
namespace {
class MipsMulMulBugFix : public MachineFunctionPass {
public:
MipsMulMulBugFix() : MachineFunctionPass(ID) {}
StringRef getPassName() const override { return "Mips VR4300 mulmul bugfix"; }
MachineFunctionProperties getRequiredProperties() const override {
return MachineFunctionProperties().setNoVRegs();
}
bool runOnMachineFunction(MachineFunction &MF) override;
static char ID;
private:
bool fixMulMulBB(MachineBasicBlock &MBB, const MipsInstrInfo &MipsII);
};
} // namespace
INITIALIZE_PASS(MipsMulMulBugFix, "mips-vr4300-mulmul-fix",
"Mips VR4300 mulmul bugfix", false, false)
char MipsMulMulBugFix::ID = 0;
bool MipsMulMulBugFix::runOnMachineFunction(MachineFunction &MF) {
const MipsInstrInfo &MipsII =
*static_cast<const MipsInstrInfo *>(MF.getSubtarget().getInstrInfo());
bool Modified = false;
for (auto &MBB : MF)
Modified |= fixMulMulBB(MBB, MipsII);
return Modified;
}
static bool isFirstMul(const MachineInstr &MI) {
switch (MI.getOpcode()) {
case Mips::FMUL_S:
case Mips::FMUL_D:
case Mips::FMUL_D32:
case Mips::FMUL_D64:
return true;
default:
return false;
}
}
static bool isSecondMulOrBranch(const MachineInstr &MI) {
if (MI.isBranch() || MI.isIndirectBranch() || MI.isCall())
return true;
switch (MI.getOpcode()) {
case Mips::MUL:
case Mips::FMUL_S:
case Mips::FMUL_D:
case Mips::FMUL_D32:
case Mips::FMUL_D64:
case Mips::MULT:
case Mips::MULTu:
case Mips::DMULT:
case Mips::DMULTu:
return true;
default:
return false;
}
}
bool MipsMulMulBugFix::fixMulMulBB(MachineBasicBlock &MBB,
const MipsInstrInfo &MipsII) {
bool Modified = false;
MachineBasicBlock::instr_iterator NextMII;
// Iterate through the instructions in the basic block
for (MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),
E = MBB.instr_end();
MII != E; MII = NextMII) {
NextMII = next_nodbg(MII, E);
// Trigger when the current instruction is a mul and the next instruction
// is either a mul or a branch in case the branch target start with a mul
if (NextMII != E && isFirstMul(*MII) && isSecondMulOrBranch(*NextMII)) {
LLVM_DEBUG(dbgs() << "Found mulmul!\n");
const MCInstrDesc &NewMCID = MipsII.get(Mips::NOP);
BuildMI(MBB, NextMII, DebugLoc(), NewMCID);
Modified = true;
}
}
return Modified;
}
FunctionPass *llvm::createMipsMulMulBugPass() { return new MipsMulMulBugFix(); }