Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions llvm/include/llvm/Support/RISCVISAUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#define LLVM_SUPPORT_RISCVISAUTILS_H

#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
#include <map>
#include <string>

Expand Down Expand Up @@ -43,6 +45,23 @@ struct ExtensionComparator {
typedef std::map<std::string, ExtensionVersion, ExtensionComparator>
OrderedExtensionMap;

enum class ZicfilpLabelSchemeKind {
Invalid,
Unlabeled,
FuncSig,
};

// See clang::getCFBranchLabelSchemeFlagVal() for possible CFBranchLabelScheme
inline ZicfilpLabelSchemeKind
getZicfilpLabelScheme(const StringRef CFBranchLabelScheme) {
const auto Ret = StringSwitch<ZicfilpLabelSchemeKind>(CFBranchLabelScheme)
.Case("unlabeled", ZicfilpLabelSchemeKind::Unlabeled)
.Case("func-sig", ZicfilpLabelSchemeKind::FuncSig)
.Default(ZicfilpLabelSchemeKind::Invalid);
assert(Ret != ZicfilpLabelSchemeKind::Invalid);
return Ret;
}

} // namespace RISCVISAUtils

} // namespace llvm
Expand Down
21 changes: 20 additions & 1 deletion llvm/lib/Target/RISCV/RISCVIndirectBranchTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/RISCVISAUtils.h"

#define DEBUG_TYPE "riscv-indrect-branch-tracking"
#define PASS_NAME "RISC-V Indirect Branch Tracking"

using namespace llvm;
using namespace llvm::RISCVISAUtils;

cl::opt<uint32_t> PreferredLandingPadLabel(
"riscv-landing-pad-label", cl::ReallyHidden,
Expand Down Expand Up @@ -64,9 +67,25 @@ static void emitLpad(MachineBasicBlock &MBB, const RISCVInstrInfo *TII,
bool RISCVIndirectBranchTracking::runOnMachineFunction(MachineFunction &MF) {
const auto &Subtarget = MF.getSubtarget<RISCVSubtarget>();
const RISCVInstrInfo *TII = Subtarget.getInstrInfo();
if (!Subtarget.hasStdExtZicfilp())

const Module *const M = MF.getFunction().getParent();
if (!M)
return false;
if (const Metadata *const Flag = M->getModuleFlag("cf-protection-branch");
!Flag || mdconst::extract<ConstantInt>(Flag)->isZero())
return false;

StringRef CFBranchLabelScheme;
if (const Metadata *const MD = M->getModuleFlag("cf-branch-label-scheme"))
CFBranchLabelScheme = cast<MDString>(MD)->getString();
else
report_fatal_error("missing cf-branch-label-scheme module flag");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use reportFatalUsageError which won't generate a stack trace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is inlined to somewhere (getSubtargetImpl()) that have multiple possible call paths, so I think it's better to print stack for this one.


const ZicfilpLabelSchemeKind Scheme =
getZicfilpLabelScheme(CFBranchLabelScheme);
if (Scheme != ZicfilpLabelSchemeKind::Unlabeled)
report_fatal_error("unsupported cf-branch-label-scheme module flag");

uint32_t FixedLabel = 0;
if (PreferredLandingPadLabel.getNumOccurrences() > 0) {
if (!isUInt<20>(PreferredLandingPadLabel))
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/RISCV/lpad.ll
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,8 @@ define void @interrupt() "interrupt"="machine" {
; FIXED-ONE-NEXT: mret
ret void
}

!llvm.module.flags = !{!0, !1}

!0 = !{i32 8, !"cf-protection-branch", i32 1}
!1 = !{i32 1, !"cf-branch-label-scheme", !"unlabeled"}
Loading