souffle  2.0.2-371-g6315b36
Transformer.cpp
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2018, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 /************************************************************************
10  *
11  * @file Transformer.cpp
12  *
13  * Defines the interface for RAM transformation passes.
14  *
15  ***********************************************************************/
16 
18 #include "Global.h"
19 #include "ram/Node.h"
20 #include "ram/Program.h"
21 #include "ram/TranslationUnit.h"
22 #include "ram/transform/Meta.h"
23 #include "reports/DebugReport.h"
24 #include "reports/ErrorReport.h"
26 #include <chrono>
27 #include <cstdlib>
28 #include <iostream>
29 #include <type_traits>
30 
31 namespace souffle::ram::transform {
32 
33 bool Transformer::apply(TranslationUnit& translationUnit) {
34  const bool debug = Global::config().has("debug-report");
35  const bool verbose = Global::config().has("verbose");
36  std::string ramProgStrOld = debug ? toString(translationUnit.getProgram()) : "";
37 
38  // invoke the transformation
40  bool changed = transform(translationUnit);
42 
43  // invalidate analyses in case the program has changed
44  if (changed) {
45  translationUnit.invalidateAnalyses();
46  }
47 
48  // print runtime & change info for transformer in verbose mode
49  if (verbose && (!isA<MetaTransformer>(this))) {
50  std::string changedString = changed ? "changed" : "unchanged";
51  std::cout << getName() << " time: " << std::chrono::duration<double>(end - start).count() << "sec ["
52  << changedString << "]" << std::endl;
53  }
54 
55  // print program after transformation in debug report
56  if (debug) {
57  translationUnit.getDebugReport().startSection();
58  if (changed) {
59  translationUnit.getDebugReport().addCodeSection(getName(), "RAM Program after " + getName(),
60  "ram", ramProgStrOld, toString(translationUnit.getProgram()));
61 
62  translationUnit.getDebugReport().endSection(getName(), getName());
63  } else {
64  translationUnit.getDebugReport().endSection(getName(), getName() + " " + " (unchanged)");
65  }
66  }
67 
68  // abort evaluation of the program if errors were encountered
69  if (translationUnit.getErrorReport().getNumErrors() != 0) {
70  std::cerr << translationUnit.getErrorReport();
71  std::cerr << translationUnit.getErrorReport().getNumErrors()
72  << " errors generated, evaluation aborted" << std::endl;
73  exit(EXIT_FAILURE);
74  }
75 
76  return changed;
77 }
78 
79 } // namespace souffle::ram::transform
souffle::ram::transform
Definition: ChoiceConversion.cpp:30
DebugReport.h
souffle::ram::transform::Transformer::transform
virtual bool transform(TranslationUnit &translationUnit)=0
@Brief transform the translation unit / used by apply @Param translationUnit that will be transformed...
souffle::ram::transform::Transformer::apply
bool apply(TranslationUnit &translationUnit)
@Brief apply the transformer to a translation unit @Param translationUnit that will be transformed.
Definition: Transformer.cpp:39
souffle::ram::transform::Transformer::getName
virtual std::string getName() const =0
@Brief get name of the transformer
Program.h
Meta.h
Global.h
souffle::toString
const std::string & toString(const std::string &str)
A generic function converting strings into strings (trivial case).
Definition: StringUtil.h:234
souffle::now
time_point now()
Definition: MiscUtil.h:89
StringUtil.h
TranslationUnit.h
Transformer.h
souffle::Global::config
static MainConfig & config()
Definition: Global.h:141
Node.h
ErrorReport.h