Skip to content

Commit

Permalink
Remove handling of attributes
Browse files Browse the repository at this point in the history
The API around attributes changed in LLVM 14. Rather than blindly upgrade the
FactGenerator to continue to type-check, I realized that

1. Attribute handling was probably already incomplete - I'm sure LLVM 10-13
   added new attributes that I hadn't yet updated the schema and FactGenerator
   to account for.
2. I wasn't really sure how attributes impacted the analysis.

I decided to try to delete everything related to attributes instead. The idea is
that if we don't use them, they're just slowing everything down and adding a
maintenance burden, even while we *know* that the current support isn't
comprehensive (and thus not useful to e.g., other clients of the FactGenerator).

This appears to pass all of the tests, including the golden tests and
`EXTRA_TESTS`. It's certainly possible that the analysis changes would impact
the output on programs that do use the `byval` attribute and that we don't have
enough coverage of these in the test suite. However, it seems comparably likely
that some aspect of the LLVM language changed in a way that made this code not
work the way it was intended to when it was originally written.
  • Loading branch information
langston-barrett committed Oct 27, 2022
1 parent ac05eaf commit 422d1a6
Show file tree
Hide file tree
Showing 18 changed files with 24 additions and 571 deletions.
17 changes: 2 additions & 15 deletions FactGenerator/include/FactGenerator.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef CSV_GENERATOR_H__
#define CSV_GENERATOR_H__

#include <llvm/IR/Attributes.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/GlobalValue.h>
Expand Down Expand Up @@ -29,12 +28,6 @@ namespace cclyzer {
class FactGenerator;
}

#if LLVM_VERSION_MAJOR < 5 // AttributeSet ->AttributeList
typedef llvm::AttributeSet Attributes;
#else
typedef llvm::AttributeList Attributes;
#endif

class cclyzer::FactGenerator : private RefmodeEngine,
private Demangler,
private ForwardingFactWriter {
Expand Down Expand Up @@ -71,8 +64,7 @@ class cclyzer::FactGenerator : private RefmodeEngine,
using pred_t = predicates::pred_t;

/* Constructor must initialize output file streams */
FactGenerator(FactWriter &writer)
: ForwardingFactWriter(writer) {}
FactGenerator(FactWriter &writer) : ForwardingFactWriter(writer) {}

/* Recording variables and types */
void recordVariable(const std::string &id, const llvm::Type *type) {
Expand All @@ -86,9 +78,6 @@ class cclyzer::FactGenerator : private RefmodeEngine,

/* Auxiliary fact writing methods */

template <typename PredGroup>
void writeFnAttributes(const refmode_t &, const Attributes);

template <typename PredGroup, class ConstantType>
void writeConstantWithOperands(const ConstantType &, const refmode_t &);

Expand Down Expand Up @@ -141,9 +130,7 @@ class cclyzer::FactGenerator : private RefmodeEngine,
gen.enterModule(m, path);
}

~ModuleContext() {
gen.exitModule();
}
~ModuleContext() { gen.exitModule(); }

private:
FactGenerator &gen;
Expand Down
15 changes: 0 additions & 15 deletions FactGenerator/include/predicates.inc
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ PREDICATE2(func, visibility)
PREDICATE2(func, calling_conv)
PREDICATE2(func, section)
PREDICATE2(func, alignment)
PREDICATE2(func, func_attr)
PREDICATE2(func, gc)
PREDICATE2(func, pers_fn)
PREDICATE2(func, name)
PREDICATE2(func, ty)
PREDICATE2(func, signature)
PREDICATE2(func, param)
PREDICATE2(func, return_attr)
PREDICATE2(func, param_attr)
GROUP_END(function)

GROUP_BEGIN(instr)
Expand Down Expand Up @@ -231,9 +228,6 @@ PREDICATE2(invoke, instr)
PREDICATEI(invoke, arg)
PREDICATEI(invoke, func_operand)
PREDICATEI(invoke, calling_conv)
PREDICATEI(invoke, return_attr)
PREDICATEI(invoke, param_attr)
PREDICATEI(invoke, func_attr)
PREDICATEI(invoke, normal_label)
PREDICATEI(invoke, exception_label)
GROUP_END(invoke)
Expand Down Expand Up @@ -451,9 +445,6 @@ PREDICATE2(call, instr)
PREDICATEI(call, func_operand)
PREDICATEI(call, arg)
PREDICATEI(call, calling_conv)
PREDICATEI(call, return_attr)
PREDICATEI(call, param_attr)
PREDICATEI(call, func_attr)
PREDICATEI(call, tail_opt)
GROUP_END(call)

Expand Down Expand Up @@ -627,12 +618,6 @@ PREDICATE(inline_asm, text, inline_asm_text)
PREDICATE(inline_asm, constraints, inline_asm_constraints)
GROUP_END(inline_asm)

// Function Attrs

GROUP_BEGIN(attr)
PREDICATE(attr, target_dependent, target_dependent_attr)
GROUP_END(attr)

// Signatures

GROUP_BEGIN(signature)
Expand Down
23 changes: 0 additions & 23 deletions FactGenerator/src/InstructionVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "InstructionVisitor.hpp"

#include <llvm/IR/Attributes.h>
#include <llvm/IR/DebugInfo.h>
#include <llvm/IR/Operator.h>

Expand Down Expand Up @@ -292,16 +291,6 @@ void InstructionVisitor::visitInvokeInst(const llvm::InvokeInst &II) {
writeInstrOperand(pred::invoke::normal_label, iref, II.getNormalDest());
writeInstrOperand(pred::invoke::exception_label, iref, II.getUnwindDest());

// Function Attributes
const Attributes &Attrs = II.getAttributes();

if (Attrs.hasAttributes(Attributes::ReturnIndex)) {
string attrs = Attrs.getAsString(Attributes::ReturnIndex);
gen.writeFact(pred::invoke::return_attr, iref, attrs);
}

gen.writeFnAttributes<pred::invoke>(iref, Attrs);

// TODO: Why not CallingConv::C
if (II.getCallingConv() != llvm::CallingConv::C) {
refmode_t cconv = gen.refmode(II.getCallingConv());
Expand Down Expand Up @@ -564,18 +553,6 @@ void InstructionVisitor::visitCallInst(const llvm::CallInst &CI) {
refmode_t cconv = gen.refmode(CI.getCallingConv());
gen.writeFact(pred::call::calling_conv, iref, cconv);
}

// Attributes
const Attributes &Attrs = CI.getAttributes();

if (Attrs.hasAttributes(Attributes::ReturnIndex)) {
const auto ReturnAttrs = Attrs.getAttributes(Attributes::ReturnIndex);
for (const auto Attr : ReturnAttrs) {
gen.writeFact(pred::call::return_attr, iref, Attr.getAsString());
}
}

gen.writeFnAttributes<pred::call>(iref, Attrs);
}

void InstructionVisitor::visitDbgDeclareInst(const llvm::DbgDeclareInst &DDI) {
Expand Down
70 changes: 0 additions & 70 deletions FactGenerator/src/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <llvm/Config/llvm-config.h>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/Function.h>

Expand Down Expand Up @@ -74,17 +73,6 @@ void FactGenerator::writeFunction(
}
#endif

// Record function attributes TODO
const Attributes &Attrs = func.getAttributes();

if (Attrs.hasAttributes(Attributes::ReturnIndex))
writeFact(
pred::func::return_attr,
funcref,
Attrs.getAsString(Attributes::ReturnIndex));

writeFnAttributes<pred::func>(funcref, Attrs);

if (func.isDeclaration()) {
// Record as a function declaration entity
writeFact(pred::func::id_decl, funcref);
Expand Down Expand Up @@ -123,61 +111,3 @@ void FactGenerator::writeFunction(
recordVariable(varId, arg->getType());
}
}

//------------------------------------------------------------------------------
// Record Function Attributes
//------------------------------------------------------------------------------

template <typename PredGroup>
void FactGenerator::writeFnAttributes(
const refmode_t &refmode, const Attributes allAttrs) {
#if LLVM_VERSION_MAJOR < 5 // AttributeSet -> AttributeList
for (unsigned i = 0; i < allAttrs.getNumSlots(); ++i) {
unsigned index = allAttrs.getSlotIndex(i);

// Write out each attribute for this slot
for (Attributes::iterator it = allAttrs.begin(i), end = allAttrs.end(i);
it != end;
++it) {
llvm::Attribute attrib = *it;
#else
// Write out each attribute for this slot
for (unsigned index = allAttrs.index_begin(), e = allAttrs.index_end();
index != e;
++index) {
llvm::AttributeSet attrs = allAttrs.getAttributes(index);
for (const llvm::Attribute attrib : attrs) {
#endif
std::string attr = attrib.getAsString();
attr.erase(std::remove(attr.begin(), attr.end(), '"'), attr.end());

// Record target-dependent attributes
if (attrib.isStringAttribute())
writeFact(pred::attr::target_dependent, attr);

// Record attribute by kind
switch (index) {
case Attributes::AttrIndex::ReturnIndex:
writeFact(PredGroup::return_attr, refmode, attr);
break;
case Attributes::AttrIndex::FunctionIndex:
writeFact(PredGroup::func_attr, refmode, attr);
break;
default:
writeFact(PredGroup::param_attr, refmode, index - 1, attr);
break;
}
}
}
}

// Instantiate template method

template void FactGenerator::writeFnAttributes<pred::func>(
const refmode_t &refmode, const Attributes Attrs);

template void FactGenerator::writeFnAttributes<pred::call>(
const refmode_t &refmode, const Attributes Attrs);

template void FactGenerator::writeFnAttributes<pred::invoke>(
const refmode_t &refmode, const Attributes Attrs);
1 change: 0 additions & 1 deletion datalog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ set(DL_SOURCES
${CMAKE_CURRENT_LIST_DIR}/schema/switch-instr.dl
${CMAKE_CURRENT_LIST_DIR}/schema/lshr-instr.dl
${CMAKE_CURRENT_LIST_DIR}/schema/fpext-instr.dl
${CMAKE_CURRENT_LIST_DIR}/schema/attr.dl
${CMAKE_CURRENT_LIST_DIR}/schema/landingpad-instr.dl
${CMAKE_CURRENT_LIST_DIR}/callgraph/entry-points.dl
${CMAKE_CURRENT_LIST_DIR}/export/subset.dl
Expand Down
74 changes: 5 additions & 69 deletions datalog/export/debug-output-extended.dl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
.output atomicrmw_instr_operation (compress=true)
.output atomicrmw_instr_ordering (compress=true)
.output atomicrmw_instr_value (compress=true)
.output attr (compress=true)
.output available_externally_linkage_type (compress=true)
.output basic_allocation (compress=true)
.output bitcast_constant_expression (compress=true)
Expand All @@ -149,15 +148,12 @@
.output br_instr_uncond_label (compress=true)
.output call_instr (compress=true)
.output call_instr_arg (compress=true)
.output call_instr_func_operand (compress=true)
.output call_instr_calling_conv (compress=true)
.output call_instr_fn_target (compress=true)
.output call_instr_fn_type (compress=true)
.output call_instr_tail_opt (compress=true)
.output call_instr_func_operand (compress=true)
.output call_instr_return_type (compress=true)
.output call_instr_calling_conv (compress=true)
.output call_instr_func_attr (compress=true)
.output call_instr_param_attr (compress=true)
.output call_instr_return_attr (compress=true)
.output call_instr_tail_opt (compress=true)
.output call_or_invoke_instr_fn_type (compress=true)
.output called_at_startup (compress=true)
.output calling_convention (compress=true)
Expand Down Expand Up @@ -321,49 +317,13 @@
.output func (compress=true)
.output func_alignment (compress=true)
.output func_by_location (compress=true)
.output func_by_value_param (compress=true)
.output func_calling_conv (compress=true)
.output func_calls_func (compress=true)
.output func_constant (compress=true)
.output func_constant_fn_name (compress=true)
.output func_decl (compress=true)
.output func_decl_to_defn (compress=true)
.output func_degree (compress=true)
.output func_func_attr (compress=true)
.output func_func_attr__alignstack (compress=true)
.output func_func_attr__alwaysinline (compress=true)
.output func_func_attr__argmemonly (compress=true)
.output func_func_attr__builtin (compress=true)
.output func_func_attr__cold (compress=true)
.output func_func_attr__convergent (compress=true)
.output func_func_attr__inaccessiblemem_or_argmemonly (compress=true)
.output func_func_attr__inaccessiblememonly (compress=true)
.output func_func_attr__inlinehint (compress=true)
.output func_func_attr__jumptable (compress=true)
.output func_func_attr__minsize (compress=true)
.output func_func_attr__naked (compress=true)
.output func_func_attr__nobuiltin (compress=true)
.output func_func_attr__noduplicate (compress=true)
.output func_func_attr__noimplicitfloat (compress=true)
.output func_func_attr__noinline (compress=true)
.output func_func_attr__nonlazybind (compress=true)
.output func_func_attr__norecurse (compress=true)
.output func_func_attr__noredzone (compress=true)
.output func_func_attr__noreturn (compress=true)
.output func_func_attr__nounwind (compress=true)
.output func_func_attr__optnone (compress=true)
.output func_func_attr__optsize (compress=true)
.output func_func_attr__readnone (compress=true)
.output func_func_attr__readonly (compress=true)
.output func_func_attr__returns_twice (compress=true)
.output func_func_attr__safestack (compress=true)
.output func_func_attr__sanitize_address (compress=true)
.output func_func_attr__sanitize_memory (compress=true)
.output func_func_attr__sanitize_thread (compress=true)
.output func_func_attr__ssp (compress=true)
.output func_func_attr__sspreq (compress=true)
.output func_func_attr__sspstrong (compress=true)
.output func_func_attr__uwtable (compress=true)
.output func_gc (compress=true)
.output func_is_illformed (compress=true)
.output func_is_wellformed (compress=true)
Expand All @@ -374,12 +334,8 @@
.output func_nparams (compress=true)
.output func_out_degree (compress=true)
.output func_param (compress=true)
.output func_param_attr (compress=true)
.output func_param_by_value (compress=true)
.output func_param_not_by_value (compress=true)
.output func_pers_fn (compress=true)
.output func_pts_signature (compress=true)
.output func_return_attr (compress=true)
.output func_returns_value (compress=true)
.output func_section (compress=true)
.output func_signature (compress=true)
Expand Down Expand Up @@ -521,16 +477,13 @@
.output inttoptr_instr_to_type (compress=true)
.output invoke_instr (compress=true)
.output invoke_instr_arg (compress=true)
.output invoke_instr_calling_conv (compress=true)
.output invoke_instr_exception_label (compress=true)
.output invoke_instr_func_operand (compress=true)
.output invoke_instr_fn_target (compress=true)
.output invoke_instr_fn_type (compress=true)
.output invoke_instr_func_operand (compress=true)
.output invoke_instr_normal_label (compress=true)
.output invoke_instr_return_type (compress=true)
.output invoke_instr_calling_conv (compress=true)
.output invoke_instr_func_attr (compress=true)
.output invoke_instr_param_attr (compress=true)
.output invoke_instr_return_attr (compress=true)
.output label_type (compress=true)
.output landingpad (compress=true)
.output landingpad_instr (compress=true)
Expand Down Expand Up @@ -562,7 +515,6 @@
.output main_func (compress=true)
.output max_context_depth (compress=true)
.output max_num_callsites (compress=true)
.output memcpy_pass_by_value (compress=true)
.output metadata_type (compress=true)
.output minimal_suffix (compress=true)
.output minimal_suffix_len (compress=true)
Expand Down Expand Up @@ -593,20 +545,6 @@
.output or_instr_first_operand (compress=true)
.output or_instr_second_operand (compress=true)
.output ordering (compress=true)
.output parameter_attr__align (compress=true)
.output parameter_attr__byval (compress=true)
.output parameter_attr__dereferenceable (compress=true)
.output parameter_attr__dereferenceable_or_null (compress=true)
.output parameter_attr__inalloca (compress=true)
.output parameter_attr__inreg (compress=true)
.output parameter_attr__nest (compress=true)
.output parameter_attr__noalias (compress=true)
.output parameter_attr__nocapture (compress=true)
.output parameter_attr__nonnull (compress=true)
.output parameter_attr__returned (compress=true)
.output parameter_attr__signext (compress=true)
.output parameter_attr__sret (compress=true)
.output parameter_attr__zeroext (compress=true)
.output path (compress=true)
.output path_component (compress=true)
.output path_component_at_any_index (compress=true)
Expand Down Expand Up @@ -713,7 +651,6 @@
.output srem_instr_second_operand (compress=true)
.output stack_allocation (compress=true)
.output stack_allocation_by_instr (compress=true)
.output stack_allocation_by_parameter (compress=true)
.output stack_allocation_by_type_instr (compress=true)
.output stack_region (compress=true)
.output startup_context (compress=true)
Expand Down Expand Up @@ -747,7 +684,6 @@
.output switch_instr_default_label (compress=true)
.output switch_instr_ncases (compress=true)
.output switch_instr_operand (compress=true)
.output target_dependent_attr (compress=true)
.output template_type (compress=true)
.output template_typeinfo (compress=true)
.output terminator_instr (compress=true)
Expand Down
Loading

0 comments on commit 422d1a6

Please sign in to comment.