Skip to content

In Which I Make a LEG Twitch

November 16, 2014

As an experiment, I injected a printf into the constructor for the LEGTargetMachine class. The purpose being to get a reaction and to see how long the build would take.

LEGTargetMachine::LEGTargetMachine(const Target &T, StringRef TT, StringRef CPU,
                                   StringRef FS, const TargetOptions &Options,
                                   Reloc::Model RM, CodeModel::Model CM,
                                   CodeGenOpt::Level OL)
    : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
      Subtarget(TT, CPU, FS, *this) {
		  printf("This is MY leg\n");
  initAsmInfo();
}

The build took 40 minutes(!) but in the end, the leg twitched nicely:

bill-rowes-MacBook:llvmcosmac billrowe$ ./build/bin/debug/clang -cc1 -triple leg-unknown-unknown -S -O3 -o ex1.s llvmlegcos/docs/Codeplay/examples/ex1/ex1.c
This is MY leg
bill-rowes-MacBook:llvmcosmac billrowe$ cat ex1.s
	.file	"llvmlegcos/docs/Codeplay/examples/ex1/ex1.c"

One other significant learning is that there is nothing in that module (LEGTargetMachine.cpp) that I would be smart enough to change at this time; nothing that deals with registers, instructions, or anything else specific. So yay that! Certainly it refers to a bunch of things that need to change (like the datalayout string) but they are defined elsewhere.

The whole content of that module and the related header file are shown below:

//===-- LEGTargetMachine.h - Define TargetMachine for LEG ---*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the LEG specific subclass of TargetMachine.
//
//===----------------------------------------------------------------------===//

#ifndef LEGTARGETMACHINE_H
#define LEGTARGETMACHINE_H

#include "LEG.h"
#include "LEGFrameLowering.h"
#include "LEGISelLowering.h"
#include "LEGInstrInfo.h"
#include "LEGSelectionDAGInfo.h"
#include "LEGSubtarget.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/Target/TargetMachine.h"

namespace llvm {

class LEGTargetMachine : public LLVMTargetMachine {
  LEGSubtarget Subtarget;

public:
  LEGTargetMachine(const Target &T, StringRef TT, StringRef CPU, StringRef FS,
                   const TargetOptions &Options, Reloc::Model RM,
                   CodeModel::Model CM, CodeGenOpt::Level OL);

  const DataLayout *getDataLayout() const LLVM_OVERRIDE;
  const InstrItineraryData *getInstrItineraryData() const LLVM_OVERRIDE;
  const LEGInstrInfo *getInstrInfo() const LLVM_OVERRIDE;
  const LEGRegisterInfo *getRegisterInfo() const LLVM_OVERRIDE;
  const LEGTargetLowering *getTargetLowering() const LLVM_OVERRIDE;
  const LEGFrameLowering *getFrameLowering() const LLVM_OVERRIDE;
  const LEGSelectionDAGInfo *getSelectionDAGInfo() const LLVM_OVERRIDE;

  virtual const LEGSubtarget *getSubtargetImpl() const { return &Subtarget; }

  // Pass Pipeline Configuration
  virtual TargetPassConfig *createPassConfig(PassManagerBase &PM);

  virtual void addAnalysisPasses(PassManagerBase &PM);
};

} // end namespace llvm

#endif

//===-- LEGTargetMachine.cpp - Define TargetMachine for LEG -----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//

#include "LEGTargetMachine.h"
#include "LEG.h"
#include "LEGFrameLowering.h"
#include "LEGInstrInfo.h"
#include "LEGISelLowering.h"
#include "LEGSelectionDAGInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/Module.h"
#include "llvm/PassManager.h"
#include "llvm/Support/TargetRegistry.h"
using namespace llvm;

LEGTargetMachine::LEGTargetMachine(const Target &T, StringRef TT, StringRef CPU,
                                   StringRef FS, const TargetOptions &Options,
                                   Reloc::Model RM, CodeModel::Model CM,
                                   CodeGenOpt::Level OL)
    : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
      Subtarget(TT, CPU, FS, *this) {
		  printf("This is MY leg\n");
  initAsmInfo();
}

const DataLayout *LEGTargetMachine::getDataLayout() const {
  return Subtarget.getDataLayout();
}

const InstrItineraryData *LEGTargetMachine::getInstrItineraryData() const {
  return Subtarget.getInstrItineraryData();
}

const LEGInstrInfo *LEGTargetMachine::getInstrInfo() const {
  return Subtarget.getInstrInfo();
}

const LEGRegisterInfo *LEGTargetMachine::getRegisterInfo() const {
  return Subtarget.getRegisterInfo();
}

const LEGTargetLowering *LEGTargetMachine::getTargetLowering() const {
  return Subtarget.getTargetLowering();
}

const LEGFrameLowering *LEGTargetMachine::getFrameLowering() const {
  return Subtarget.getFrameLowering();
}

const LEGSelectionDAGInfo *LEGTargetMachine::getSelectionDAGInfo() const {
  return Subtarget.getSelectionDAGInfo();
}

namespace {
/// LEG Code Generator Pass Configuration Options.
class LEGPassConfig : public TargetPassConfig {
public:
  LEGPassConfig(LEGTargetMachine *TM, PassManagerBase &PM)
      : TargetPassConfig(TM, PM) {}

  LEGTargetMachine &getLEGTargetMachine() const {
    return getTM<LEGTargetMachine>();
  }

  virtual bool addPreISel();
  virtual bool addInstSelector();
  virtual bool addPreEmitPass();
};
} // namespace

TargetPassConfig *LEGTargetMachine::createPassConfig(PassManagerBase &PM) {
  return new LEGPassConfig(this, PM);
}

bool LEGPassConfig::addPreISel() { return false; }

bool LEGPassConfig::addInstSelector() {
  addPass(createLEGISelDag(getLEGTargetMachine(), getOptLevel()));
  return false;
}

bool LEGPassConfig::addPreEmitPass() { return false; }

// Force static initialization.
extern "C" void LLVMInitializeLEGTarget() {
  RegisterTargetMachine<LEGTargetMachine> X(TheLEGTarget);
}

void LEGTargetMachine::addAnalysisPasses(PassManagerBase &PM) {}

Advertisements

From → LLVM

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: