Skip to content

Commit

Permalink
Refactor SPCP setting propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
zacikpa committed Jul 21, 2023
1 parent 2f1e696 commit bee54b7
Show file tree
Hide file tree
Showing 18 changed files with 330 additions and 106 deletions.
29 changes: 16 additions & 13 deletions diffkemp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,30 +136,33 @@ def make_argument_parser():
(even semantically equivalent ones)",
action="store_true")

BUILTIN_PATTERNS = ["struct-alignment",
"function-splits",
"unused-returns",
"kernel-prints",
"dead-code",
"numerical-macros",
"relocations",
"type-casts",
"control-flow-only",]

# Semantic patterns options.
compare_ap.add_argument("--enable-pattern",
action="append", default=[],
choices=["struct-alignment", "function-splits",
"unused-returns", "kernel-prints",
"dead-code", "numerical-macros",
"type-casts", "control-flow-only"],
help="choose which semantic patterns should be"
choices=BUILTIN_PATTERNS,
help="choose which built-in patterns should be"
"explicitly enabled")
compare_ap.add_argument("--disable-pattern",
action="append", default=[],
choices=["struct-alignment", "function-splits",
"unused-returns", "kernel-prints",
"dead-code", "numerical-macros",
"type-casts", "control-flow-only"],
help="choose which semantic patterns should be"
choices=BUILTIN_PATTERNS,
help="choose which built-in patterns should be"
"explicitly disabled")
compare_ap.add_argument("--enable-all-patterns", action="append_const",
dest="enable_pattern", const="all",
help="enable all supported semantic patterns")
help="enable all supported built-in patterns")
compare_ap.add_argument("--disable-all-patterns", action="append_const",
dest="disable_pattern", const="all",
help="disable all semantic patterns (only do pure"
"syntax diff)")
help="disable all built-in patterns")

compare_ap.set_defaults(func=diffkemp.diffkemp.compare)
return ap
3 changes: 2 additions & 1 deletion diffkemp/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def __init__(
:param full_diff: Evaluate semantics-preserving syntax differences too.
:param output_llvm_ir: Output each simplified module into a file.
:param custom_pattern_config: Valid custom pattern configuration.
:param enabled_patterns: List of enabled built-in patterns.
:param disabled_patterns: List of disabled built-in patterns.
:param print_asm_diffs: Print assembly differences.
:param control_flow_only: Check only for control-flow differences.
:param verbosity: Verbosity level (currently boolean).
:param use_ffi: Use Python FFI to call SimpLL.
:param semdiff_tool: Tool to use for semantic diff.
Expand Down
76 changes: 75 additions & 1 deletion diffkemp/simpll/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ Config::Config(std::string FirstFunName,
std::string SecondOutFile,
std::string CacheDir,
std::string CustomPatternConfigPath,
builtin_pattern_args BuiltinPatternArgs,
std::string Variable,
bool OutputLlvmIR,
bool ControlFlowOnly,
bool PrintAsmDiffs,
bool PrintCallStacks,
int Verbosity)
Expand Down Expand Up @@ -75,9 +75,83 @@ Config::Config(std::string FirstFunName,
}
}
setDebugTypes(debugTypes);

BuiltinPatterns = parseBuiltinPatternArgs(BuiltinPatternArgs);
}

void Config::refreshFunctions() {
FirstFun = First->getFunction(FirstFunName);
SecondFun = Second->getFunction(SecondFunName);
}

BuiltinPatterns parseBuiltinPatternArgs(const builtin_pattern_args &Args) {
struct BuiltinPatterns Patterns = DEFAULT_BUILTIN_PATTERNS;

// Set built-in patterns to use. Priorities are as follows:
// 1. Explicit disablement of a single pattern.
// 2. Explicit enablement of a single pattern.
// 3. Explicit disablement of all patterns.
// 4. Explicit enablement of all patterns.
// 5. Default settings (see Config.h)
if (Args.EnableAll) {
Patterns.StructAlignment = true;
Patterns.FunctionSplits = true;
Patterns.UnusedReturnTypes = true;
Patterns.KernelPrints = true;
Patterns.DeadCode = true;
Patterns.NumericalMacros = true;
Patterns.TypeCasts = true;
Patterns.ControlFlowOnly = true;
}
if (Args.DisableAll) {
Patterns.StructAlignment = false;
Patterns.FunctionSplits = false;
Patterns.UnusedReturnTypes = false;
Patterns.KernelPrints = false;
Patterns.DeadCode = false;
Patterns.NumericalMacros = false;
Patterns.TypeCasts = false;
Patterns.ControlFlowOnly = false;
}
if (Args.EnableStructAlignment)
Patterns.StructAlignment = true;
if (Args.DisableStructAlignment)
Patterns.StructAlignment = false;
if (Args.EnableFunctionSplits)
Patterns.FunctionSplits = true;
if (Args.DisableFunctionSplits)
Patterns.FunctionSplits = false;
if (Args.EnableUnusedReturnTypes)
Patterns.UnusedReturnTypes = true;
if (Args.DisableUnusedReturnTypes)
Patterns.UnusedReturnTypes = false;
if (Args.EnableKernelPrints)
Patterns.KernelPrints = true;
if (Args.DisableKernelPrints)
Patterns.KernelPrints = false;
if (Args.EnableDeadCode)
Patterns.DeadCode = true;
if (Args.DisableDeadCode)
Patterns.DeadCode = false;
if (Args.EnableNumericalMacros)
Patterns.NumericalMacros = true;
if (Args.DisableNumericalMacros)
Patterns.NumericalMacros = false;
if (Args.EnableRelocations)
Patterns.Relocations = true;
if (Args.DisableRelocations)
Patterns.Relocations = false;
if (Args.EnableTypeCasts)
Patterns.TypeCasts = true;
if (Args.DisableTypeCasts)
Patterns.TypeCasts = false;
if (Args.EnableControlFlowOnly) {
Patterns.ControlFlowOnly = true;
// Turning on control-flow-only automatically turns on type casts
Patterns.TypeCasts = true;
}
if (Args.DisableControlFlowOnly)
Patterns.ControlFlowOnly = false;

return Patterns;
}
82 changes: 47 additions & 35 deletions diffkemp/simpll/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef DIFFKEMP_SIMPLL_CONFIG_H
#define DIFFKEMP_SIMPLL_CONFIG_H

#include "library/FFI.h"
#include "llvm/Support/CommandLine.h"
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
Expand All @@ -26,6 +27,44 @@

using namespace llvm;

struct BuiltinPatterns {
bool StructAlignment;
bool FunctionSplits;
bool UnusedReturnTypes;
bool KernelPrints;
bool DeadCode;
bool NumericalMacros;
bool Relocations;
bool TypeCasts;
bool ControlFlowOnly;
};

const BuiltinPatterns DEFAULT_BUILTIN_PATTERNS = {
// Changes in structure alignment
.StructAlignment = true,
// Splitting code into functions
.FunctionSplits = true,
// Changing unused return values to void
.UnusedReturnTypes = true,
// Changes in kernel-specific printing functions calls. These include:
// - changes in strings printed by kernel print functions
// - changes in arguments of kernel functions that are related to the
// call location (file name and line number)
// - changes in counter, date, time, file name, and line macros
.KernelPrints = true,
// Changes in dead code
.DeadCode = true,
// Changed numerical value of a macro
.NumericalMacros = true,
// Relocated instructions
.Relocations = true,
// Changes in type casts
.TypeCasts = false,
// Ignore all changes except those in control-flow
.ControlFlowOnly = false};

BuiltinPatterns parseBuiltinPatternArgs(const builtin_pattern_args &args);

/// Tool configuration parsed from CLI options.
class Config {
private:
Expand Down Expand Up @@ -54,43 +93,17 @@ class Config {
// Path to custom LLVM IR differential pattern configuration.
std::string CustomPatternConfigPath;

// The following structure specifies which built-in patterns
// should be treated as semantically equal.
struct BuiltinPatterns BuiltinPatterns;

// Save the simplified IR of the module to a file.
bool OutputLlvmIR;
// Print raw differences in inline assembly.
bool PrintAsmDiffs;
// Show call stacks for non-equal functions
bool PrintCallStacks;

// The following boolean variables specify which patterns of syntactic
// changes should be treated as semantically equal.

// Patterns that are known to be semantically equal (turned on by default):

// Changes in structure alignment
bool PatternStructAlignment = true;
// Splitting code into functions
bool PatternFunctionSplits = true;
// Changing unused return values to void
bool PatternUnusedReturnTypes = true;
// Changes in kernel-specific printing functions calls. These include:
// - changes in strings printed by kernel print functions
// - changes in arguments of kernel functions that are related to the call
// call location (file name and line number)
// - changes in counter, date, time, file name, and line macros
bool PatternKernelPrints = true;
// Changes in dead code
bool PatternDeadCode = true;
// Changed numerical value of a macro
bool PatternNumericalMacros = true;

// Patterns that are not semantically equal (turned off by default):

// Changes in type casts
bool PatternTypeCasts = false;
// Ignore all changes except those in control-flow
bool PatternControlFlowOnly = false;

// Constructor for other use than from the command line.
Config(std::string FirstFunName,
std::string SecondFunName,
Module *FirstModule,
Expand All @@ -99,27 +112,26 @@ class Config {
std::string SecondOutFile,
std::string CacheDir,
std::string CustomPatternConfigPath,
builtin_pattern_args BuiltinPatternArgs,
std::string Variable = "",
bool OutputLlvmIR = false,
bool ControlFlowOnly = false,
bool PrintAsmDiffs = true,
bool PrintCallStacks = true,
int Verbosity = 0);

// Constructor without module loading (for tests).
Config(std::string FirstFunName,
std::string SecondFunName,
std::string CacheDir,
bool ControlFlowOnly = false,
std::string CustomPatternConfigPath,
bool PrintAsmDiffs = true,
bool PrintCallStacks = true)
: FirstFunName(FirstFunName), SecondFunName(SecondFunName),
First(nullptr), Second(nullptr), FirstOutFile("/dev/null"),
SecondOutFile("/dev/null"), CacheDir(CacheDir),
PatternControlFlowOnly(ControlFlowOnly),
PrintAsmDiffs(PrintAsmDiffs),
PrintCallStacks(PrintCallStacks) {}
CustomPatternConfigPath(CustomPatternConfigPath),
BuiltinPatterns(DEFAULT_BUILTIN_PATTERNS), OutputLlvmIR(false),
PrintAsmDiffs(PrintAsmDiffs), PrintCallStacks(PrintCallStacks) {}

/// Sets debug types specified in the vector.
void setDebugTypes(std::vector<std::string> &debugTypes);
Expand Down
4 changes: 2 additions & 2 deletions diffkemp/simpll/DebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class DebugInfo {
DebugInfoFirst.processModule(ModFirst);
DebugInfoSecond.processModule(ModSecond);
// Use debug info to gather useful information
if (config.PatternStructAlignment)
if (config.BuiltinPatterns.StructAlignment)
calculateGEPIndexAlignments();
if (config.PatternNumericalMacros)
if (config.BuiltinPatterns.NumericalMacros)
calculateMacroAlignments();
collectLocalVariables(CalledFirst, LocalVariableMapL);
collectLocalVariables(CalledSecond, LocalVariableMapR);
Expand Down
Loading

0 comments on commit bee54b7

Please sign in to comment.