Testing selection dag combines

Hi,

I’ve written a few target specific dag combine functions, called from performDAGCombine. The mechanism is great but my testing quite labour intensive - I search for IR which hits the new function then check the MIR downstream.

I would like to write tests along the lines of “given this dag, call this function on that node, then check the result is equivalent to this other dag”. Could you point me at the appropriate API for creating a dag from scratch, and for performing an equivalence test?

Thanks,

Jon

I’ve successfully constructed an instance of a SelectionDAG outside of the back end. Actually using it segfaults though - I’ll paste the code here in the hope that I’m just missing some setup. Suggestions very welcome.

Thanks!

#include “llvm/Target/OOT/OOTLowering.h”

#include “llvm/CodeGen/SelectionDAG.h”
#include “llvm/MC/SubtargetFeature.h”
#include “llvm/Support/TargetRegistry.h”
#include “llvm/Support/TargetSelect.h”
#include “llvm/Target/TargetLowering.h”
#include “llvm/Target/TargetMachine.h”

namespace llvm {
extern Target TheOOTTarget;
}

const llvm::TargetMachine &get_target_machine() {
static bool init = false;
static llvm::TargetMachine *local = nullptr;
if (!init) {
init = true;
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
llvm::InitializeAllAsmParsers();

std::string TripleStr = llvm::sys::getDefaultTargetTriple();
llvm::Triple Triple(TripleStr);

std::string errMsg;
const llvm::Target *march =
llvm::TargetRegistry::lookupTarget(TripleStr, errMsg);
assert(march);

local = llvm::TheOOTTarget.createTargetMachine(
TripleStr, “”, “”, llvm::TargetOptions(), llvm::None);
}
assert(local);
return *local;
}

int main() {
using namespace llvm;
SelectionDAG DAG(get_target_machine(), CodeGenOpt::Default);

DAG.dump(); // OK

auto rt = DAG.getRoot();
SDLoc dl(rt);

SDValue aconstant = DAG.getConstant(123, dl, MVT::i32);
aconstant.dump(); // segv
}