Skip to content

Commit

Permalink
Add a statistic for counting {must,no}_preserve_cheri_tags attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
arichardson committed Aug 16, 2022
1 parent a5c94c8 commit c169ab1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
24 changes: 8 additions & 16 deletions llvm/include/llvm/IR/IntrinsicInst.h
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,12 @@ static inline PreserveCheriTags shouldPreserveTags(const CallInst *CI) {
return PreserveCheriTags::Unknown;
}

/// Out-of-line to allow counting statistics. This is only possible in source
/// files and MemTransferBase is templated so moving it out-of-line is more
/// awkward.
void setPreserveCheriTags(IntrinsicInst *I, PreserveCheriTags NewValue,
const DataLayout &DL);

/// Common base class for all memory transfer intrinsics. Simply provides
/// common methods.
template <class BaseCL> class MemTransferBase : public BaseCL {
Expand Down Expand Up @@ -751,22 +757,8 @@ template <class BaseCL> class MemTransferBase : public BaseCL {
.getValueAsString();
}

void setPreserveCheriTags(PreserveCheriTags NewValue) {
if (NewValue == PreserveCheriTags::Required) {
assert(!BaseCL::hasFnAttr(Attribute::NoPreserveCheriTags) &&
"attempting to set conflicting attributes");
BaseCL::addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::MustPreserveCheriTags);
} else if (NewValue == PreserveCheriTags::Unnecessary) {
assert(!BaseCL::hasFnAttr(Attribute::MustPreserveCheriTags) &&
"attempting to set conflicting attributes");
BaseCL::addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::NoPreserveCheriTags);
} else {
assert(!BaseCL::hasFnAttr(Attribute::MustPreserveCheriTags) &&
!BaseCL::hasFnAttr(Attribute::NoPreserveCheriTags) &&
"Cannot set unknown on an already annotated instruction");
}
void setPreserveCheriTags(PreserveCheriTags NewValue, const DataLayout &DL) {
llvm::setPreserveCheriTags(this, NewValue, DL);
}
};

Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ CallInst *IRBuilderBase::CreateMemTransferInst(

auto* MCI = cast<MemTransferInst>(CI);
if (M->getDataLayout().hasCheriCapabilities())
MCI->setPreserveCheriTags(PreserveTags);
MCI->setPreserveCheriTags(PreserveTags, M->getDataLayout());
if (DstAlign)
MCI->setDestAlignment(*DstAlign);
if (SrcAlign)
Expand Down Expand Up @@ -247,7 +247,7 @@ CallInst *IRBuilderBase::CreateMemCpyInline(

auto *MCI = cast<MemCpyInlineInst>(CI);
if (M->getDataLayout().hasCheriCapabilities())
MCI->setPreserveCheriTags(PreserveTags);
MCI->setPreserveCheriTags(PreserveTags, M->getDataLayout());
if (DstAlign)
MCI->setDestAlignment(*DstAlign);
if (SrcAlign)
Expand Down Expand Up @@ -292,7 +292,7 @@ CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemCpy(
// Set the alignment of the pointer args.
auto *AMCI = cast<AtomicMemCpyInst>(CI);
if (M->getDataLayout().hasCheriCapabilities())
AMCI->setPreserveCheriTags(PreserveTags);
AMCI->setPreserveCheriTags(PreserveTags, M->getDataLayout());
AMCI->setDestAlignment(DstAlign);
AMCI->setSourceAlignment(SrcAlign);

Expand Down Expand Up @@ -335,7 +335,7 @@ CallInst *IRBuilderBase::CreateMemMove(Value *Dst, MaybeAlign DstAlign,

auto *MMI = cast<MemMoveInst>(CI);
if (M->getDataLayout().hasCheriCapabilities())
MMI->setPreserveCheriTags(PreserveTags);
MMI->setPreserveCheriTags(PreserveTags, M->getDataLayout());
if (DstAlign)
MMI->setDestAlignment(*DstAlign);
if (SrcAlign)
Expand Down Expand Up @@ -373,7 +373,8 @@ CallInst *IRBuilderBase::CreateElementUnorderedAtomicMemMove(

CallInst *CI = createCallHelper(TheFn, Ops, this);
if (M->getDataLayout().hasCheriCapabilities())
cast<AtomicMemTransferInst>(CI)->setPreserveCheriTags(PreserveTags);
cast<AtomicMemTransferInst>(CI)->setPreserveCheriTags(PreserveTags,
M->getDataLayout());

// Set the alignment of the pointer args.
CI->addParamAttr(0, Attribute::getWithAlignment(CI->getContext(), DstAlign));
Expand Down
30 changes: 30 additions & 0 deletions llvm/lib/IR/IntrinsicInst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/IntrinsicInst.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
Expand All @@ -34,6 +35,12 @@
#include "llvm/Support/raw_ostream.h"
using namespace llvm;

#define DEBUG_TYPE "intrinsics"
STATISTIC(NumMustPreserveTagAttrs,
"Number of must_preserve_cheri_tags attributes specified");
STATISTIC(NumNoPreserveTagAttrs,
"Number of no_preserve_cheri_tags attributes specified");

//===----------------------------------------------------------------------===//
/// DbgVariableIntrinsic - This is the common base class for debug info
/// intrinsics for variables.
Expand Down Expand Up @@ -544,6 +551,29 @@ unsigned BinaryOpIntrinsic::getNoWrapKind() const {
return OverflowingBinaryOperator::NoUnsignedWrap;
}

void llvm::setPreserveCheriTags(IntrinsicInst *I, PreserveCheriTags NewValue,
const DataLayout &DL) {
if (NewValue == PreserveCheriTags::Required) {
assert(DL.hasCheriCapabilities());
assert(!I->hasFnAttr(Attribute::NoPreserveCheriTags) &&
"attempting to set conflicting attributes");
I->addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::MustPreserveCheriTags);
NumMustPreserveTagAttrs++;
} else if (NewValue == PreserveCheriTags::Unnecessary) {
assert(!I->hasFnAttr(Attribute::MustPreserveCheriTags) &&
"attempting to set conflicting attributes");
assert(DL.hasCheriCapabilities());
I->addAttribute(llvm::AttributeList::FunctionIndex,
llvm::Attribute::NoPreserveCheriTags);
NumNoPreserveTagAttrs++;
} else {
assert(!I->hasFnAttr(Attribute::MustPreserveCheriTags) &&
!I->hasFnAttr(Attribute::NoPreserveCheriTags) &&
"Cannot set unknown on an already annotated instruction");
}
}

const GCStatepointInst *GCProjectionInst::getStatepoint() const {
const Value *Token = getArgOperand(0);

Expand Down

0 comments on commit c169ab1

Please sign in to comment.