Skip to content

Commit

Permalink
Rename Patterns to CustomPatterns to distinguish them from SPCPs
Browse files Browse the repository at this point in the history
  • Loading branch information
zacikpa committed Jul 21, 2023
1 parent 8051966 commit eb2be76
Show file tree
Hide file tree
Showing 68 changed files with 204 additions and 195 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ kernel/
*snapshots*/

# Difference patterns directories
patterns/
!tests/regression/patterns**
custom_patterns/
!tests/regression/custom_patterns**

# LLReve tool
llreve/
Expand Down
4 changes: 2 additions & 2 deletions diffkemp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ def make_argument_parser():
help="specify root dirs for the compared projects")
compare_ap.add_argument("--function", "-f",
help="compare only selected function")
compare_ap.add_argument("--patterns", "-p",
help="difference pattern file or configuration")
compare_ap.add_argument("--custom-patterns", "-p",
help="custom pattern file or configuration")
compare_ap.add_argument("--output-llvm-ir",
help="output each simplified module to a file",
action="store_true")
Expand Down
18 changes: 10 additions & 8 deletions diffkemp/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Configuration of the tool."""
from diffkemp.semdiff.pattern_config import PatternConfig
from diffkemp.semdiff.custom_pattern_config import CustomPatternConfig
from diffkemp.snapshot import Snapshot
import os

Expand All @@ -16,9 +16,9 @@ def __init__(
show_diff=True,
full_diff=False,
output_llvm_ir=False,
custom_pattern_config=None,
enabled_patterns=[],
disabled_patterns=[],
pattern_config=None,
print_asm_diffs=False,
verbosity=0,
use_ffi=False,
Expand All @@ -31,7 +31,7 @@ def __init__(
:param show_diff: Evaluate syntax differences.
:param full_diff: Evaluate semantics-preserving syntax differences too.
:param output_llvm_ir: Output each simplified module into a file.
:param pattern_config: Valid custom pattern configuration.
: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.
Expand All @@ -45,7 +45,7 @@ def __init__(
self.show_diff = show_diff
self.full_diff = full_diff
self.output_llvm_ir = output_llvm_ir
self.pattern_config = pattern_config
self.custom_pattern_config = custom_pattern_config
self.enabled_patterns = enabled_patterns
self.disabled_patterns = disabled_patterns
self.print_asm_diffs = print_asm_diffs
Expand Down Expand Up @@ -78,17 +78,19 @@ def from_args(cls, args):

# Transform difference pattern files into an LLVM IR
# based configuration.
if args.patterns:
pattern_config = PatternConfig.create_from_file(args.patterns)
if args.custom_patterns:
custom_pattern_config = CustomPatternConfig.create_from_file(
args.custom_patterns
)
else:
pattern_config = None
custom_pattern_config = None

return cls(
snapshot_first=snapshot_first,
snapshot_second=snapshot_second,
show_diff=not args.no_show_diff or args.show_diff,
full_diff=args.full_diff,
pattern_config=pattern_config,
custom_pattern_config=custom_pattern_config,
enabled_patterns=args.enable_pattern,
disabled_patterns=args.disable_pattern,
output_llvm_ir=args.output_llvm_ir,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import yaml


class UnsupportedPatternError(ValueError):
class UnsupportedCustomPatternError(ValueError):
"""Indicates that a pattern file uses an unsupported format."""
pass


class PatternConfig:
class CustomPatternConfig:
"""
Configuration file containing difference code patterns and global pattern
comparison settings.
Configuration file containing custom difference code patterns
and global pattern comparison settings.
Difference patterns can be used during module comparison to prevent
the reporting of known and desired semantic differences.
"""
Expand Down Expand Up @@ -41,11 +41,11 @@ def create_from_file(cls, path, patterns_path=None):
file or a configuration YAML.
:param path: Path to the file to load.
"""
PatternConfig.raise_for_invalid_file(path)
CustomPatternConfig.raise_for_invalid_file(path)
config = cls(path, patterns_path)

# Process the configuration file based on its extension.
if PatternConfig.get_extension(path) not in ("ll"):
if CustomPatternConfig.get_extension(path) not in ("ll"):
config._load_yaml()
else:
config.add_pattern(path)
Expand Down Expand Up @@ -81,8 +81,8 @@ def add_pattern(self, path):
else:
full_path = path

PatternConfig.raise_for_invalid_file(full_path)
filetype = PatternConfig.get_extension(full_path)
CustomPatternConfig.raise_for_invalid_file(full_path)
filetype = CustomPatternConfig.get_extension(full_path)

# Convert all supported pattern types to LLVM IR.
# Currently, only LLVM IR patterns are supported.
Expand Down Expand Up @@ -136,6 +136,6 @@ def _on_parse_failure(self, message):
:param message: Message for the user.
"""
if self.settings["on_parse_failure"] == "ERROR":
raise UnsupportedPatternError(message)
raise UnsupportedCustomPatternError(message)
elif self.settings["on_parse_failure"] == "WARN":
print(message)
7 changes: 3 additions & 4 deletions diffkemp/simpll/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Config::Config(std::string FirstFunName,
std::string FirstOutFile,
std::string SecondOutFile,
std::string CacheDir,
std::string PatternConfigPath,
std::string CustomPatternConfigPath,
builtin_pattern_args BuiltinPatternArgs,
std::string Variable,
bool OutputLlvmIR,
Expand All @@ -48,9 +48,8 @@ Config::Config(std::string FirstFunName,
: FirstFunName(FirstFunName), SecondFunName(SecondFunName),
First(FirstModule), Second(SecondModule), FirstOutFile(FirstOutFile),
SecondOutFile(SecondOutFile), CacheDir(CacheDir),
PatternConfigPath(PatternConfigPath),
OutputLlvmIR(OutputLlvmIR),
PrintAsmDiffs(PrintAsmDiffs),
CustomPatternConfigPath(CustomPatternConfigPath),
OutputLlvmIR(OutputLlvmIR), PrintAsmDiffs(PrintAsmDiffs),
PrintCallStacks(PrintCallStacks) {
refreshFunctions();

Expand Down
10 changes: 5 additions & 5 deletions diffkemp/simpll/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class Config {
std::string SecondOutFile;
// Cache file directory.
std::string CacheDir;
// Path to LLVM IR pattern configuration.
std::string PatternConfigPath;
// 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.
Expand All @@ -114,7 +114,7 @@ class Config {
std::string FirstOutFile,
std::string SecondOutFile,
std::string CacheDir,
std::string PatternConfigPath,
std::string CustomPatternConfigPath,
builtin_pattern_args BuiltinPatternArgs,
std::string Variable = "",
bool OutputLlvmIR = false,
Expand All @@ -126,13 +126,13 @@ class Config {
Config(std::string FirstFunName,
std::string SecondFunName,
std::string CacheDir,
std::string PatternConfigPath,
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),
PatternConfigPath(PatternConfigPath),
CustomPatternConfigPath(CustomPatternConfigPath),
BuiltinPatterns(DEFAULT_BUILTIN_PATTERNS), OutputLlvmIR(false),
PrintAsmDiffs(PrintAsmDiffs), PrintCallStacks(PrintCallStacks) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===------------- PatternComparator.h - Code pattern finder --------------===//
//===---------- CustomPatternComparator.h - Code pattern finder -----------===//
//
// SimpLL - Program simplifier for analysis of semantic difference //
//
Expand All @@ -14,7 +14,7 @@
///
//===----------------------------------------------------------------------===//

#include "PatternComparator.h"
#include "CustomPatternComparator.h"
#include "Config.h"
#include "DifferentialFunctionComparator.h"
#include "Logger.h"
Expand All @@ -24,14 +24,14 @@
/// instruction pair. Returns true if a valid match is found. Instruction
/// patterns are prioritized over value patterns. Only a single pattern
/// match is expected to be possible at once.
bool PatternComparator::matchPattern(const Instruction *InstL,
const Instruction *InstR) {
bool CustomPatternComparator::matchPattern(const Instruction *InstL,
const Instruction *InstR) {
return matchInstPattern(InstL, InstR) || matchValuePattern(InstL, InstR);
}

/// Tries to match a pair of values to a value pattern. Returns true if a
/// valid match is found.
bool PatternComparator::matchValues(const Value *L, const Value *R) {
bool CustomPatternComparator::matchValues(const Value *L, const Value *R) {
// Try to match the difference to a value-based pattern.
for (auto &&ValuePatternCompPair : ValuePatternComps) {
auto PatternComps = &ValuePatternCompPair.second;
Expand Down Expand Up @@ -59,8 +59,8 @@ bool PatternComparator::matchValues(const Value *L, const Value *R) {

/// Tries to match one of the loaded instruction patterns. Returns true
/// if a valid match is found.
bool PatternComparator::matchInstPattern(const Instruction *InstL,
const Instruction *InstR) {
bool CustomPatternComparator::matchInstPattern(const Instruction *InstL,
const Instruction *InstR) {
// Try to find an instruction-based pattern matching the starting
// instruction pair.
for (auto &&InstPatternCompPair : InstPatternComps) {
Expand Down Expand Up @@ -92,8 +92,8 @@ bool PatternComparator::matchInstPattern(const Instruction *InstL,

/// Tries to match one of the loaded value patterns. Returns true if
/// a valid match is found.
bool PatternComparator::matchValuePattern(const Instruction *InstL,
const Instruction *InstR) {
bool CustomPatternComparator::matchValuePattern(const Instruction *InstL,
const Instruction *InstR) {
// Ensure that a load instruction has been given. Value differences
// in other kinds of instructions are handled separately during
// standard value comparison.
Expand Down Expand Up @@ -135,9 +135,9 @@ bool PatternComparator::matchValuePattern(const Instruction *InstL,

/// Tries to match a load instruction to the start of the given value
/// pattern.
bool PatternComparator::matchLoadInst(const LoadInst *Load,
const ValuePattern *Pat,
bool IsLeftSide) {
bool CustomPatternComparator::matchLoadInst(const LoadInst *Load,
const ValuePattern *Pat,
bool IsLeftSide) {
// Ensure that the load instruction is valid.
if (!Load) {
return false;
Expand All @@ -159,7 +159,7 @@ bool PatternComparator::matchLoadInst(const LoadInst *Load,
/// Check whether the input mapping generated by the given pattern function
/// comparator pair is valid even when both compared modules are analysed at
/// once.
bool PatternComparator::inputMappingValid(
bool CustomPatternComparator::inputMappingValid(
const InstPattern *Pat, const InstPatternComparatorPair *PatternComps) {
for (auto &&ArgPair : Pat->ArgumentMapping) {
// Find the mapped values. Values that could not get matched during
Expand Down Expand Up @@ -215,7 +215,7 @@ bool PatternComparator::inputMappingValid(

/// Create the resulting instruction mapping and add all matched
/// instructions into the combined instruction set.
void PatternComparator::processPatternMatch(
void CustomPatternComparator::processPatternMatch(
const InstPattern *Pat, const InstPatternComparatorPair *PatternComps) {
for (auto &&InstPair : PatternComps->first->InstMatchMap) {
// Add the matched instruction into the set of matched instructions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===------------- PatternComparator.h - Code pattern finder --------------===//
//===---------- CustomPatternComparator.h - Code pattern finder -----------===//
//
// SimpLL - Program simplifier for analysis of semantic difference //
//
Expand All @@ -13,11 +13,11 @@
///
//===----------------------------------------------------------------------===//

#ifndef DIFFKEMP_SIMPLL_PATTERNCOMPARATOR_H
#define DIFFKEMP_SIMPLL_PATTERNCOMPARATOR_H
#ifndef DIFFKEMP_SIMPLL_CUSTOMPATTERNCOMPARATOR_H
#define DIFFKEMP_SIMPLL_CUSTOMPATTERNCOMPARATOR_H

#include "CustomPatternSet.h"
#include "InstPatternComparator.h"
#include "PatternSet.h"
#include "ValuePatternComparator.h"
#include <llvm/IR/Instructions.h>
#include <unordered_map>
Expand All @@ -35,12 +35,13 @@ using namespace llvm;
/// original and the modified value are described strictly through return
/// instructions.
/// Concrete examples of difference patterns can be seen in regression tests.
class PatternComparator {
class CustomPatternComparator {
public:
PatternComparator(const PatternSet *Patterns,
const DifferentialFunctionComparator *DiffFunctionComp,
const Function *FnL,
const Function *FnR)
CustomPatternComparator(
const CustomPatternSet *Patterns,
const DifferentialFunctionComparator *DiffFunctionComp,
const Function *FnL,
const Function *FnR)
: DiffFunctionComp(DiffFunctionComp) {
// Populate both pattern function comparator maps.
for (auto &InstPattern : Patterns->InstPatterns) {
Expand Down Expand Up @@ -129,4 +130,4 @@ class PatternComparator {
const InstPatternComparatorPair *PatternComps);
};

#endif // DIFFKEMP_SIMPLL_PATTERNCOMPARATOR_H
#endif // DIFFKEMP_SIMPLL_CUSTOMPATTERNCOMPARATOR_H
Loading

0 comments on commit eb2be76

Please sign in to comment.