CMakeLists.txt 9.58 KB
Newer Older
Tim Gymnich's avatar
Tim Gymnich committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 3.4.3)

project(PAIN
  VERSION 1.0.0
  LANGUAGES C CXX
)

set(CMAKE_CXX_STANDARD 17)

set (PROJECT_DESCRIPTION "Implements an LLVM analysis pass using abstract interpretation.")
set (PROJECT_HOMEPAGE_URL "https://versioncontrolseidl.in.tum.de/petter/llvm-abstractinterpretation")

if (NOT PATH_TO_LLVM)
  message(FATAL_ERROR " 
  The cmake is supposed to be called with PATH_TO_LLVM pointing to
 a precompiled version of LLVM or to to the source code of LLVM
 Examples:
 cmake -G \"${CMAKE_GENERATOR}\" -DPATH_TO_LLVM=/opt/llvm-9.0.1 ${CMAKE_SOURCE_DIR}
 cmake -G \"${CMAKE_GENERATOR}\" -DPATH_TO_LLVM=/llvm-project/llvm ${CMAKE_SOURCE_DIR}
")
Florian Stamer's avatar
Florian Stamer committed
21
endif()
Administrator's avatar
initial  
Administrator committed
22

Tim Gymnich's avatar
Tim Gymnich committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
if (NOT IS_ABSOLUTE ${PATH_TO_LLVM})
  # Convert relative path to absolute path
  get_filename_component(PATH_TO_LLVM
    "${PATH_TO_LLVM}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
endif()

set (BUILD_AGAINST_PRECOMPILED_LLVM TRUE)
if (EXISTS ${PATH_TO_LLVM}/CMakeLists.txt)
  set (BUILD_AGAINST_PRECOMPILED_LLVM FALSE)
endif()

# This enables assertions for Release builds.
# https://stackoverflow.com/questions/22140520/how-to-enable-assert-in-cmake-release-mode
string(REPLACE "-DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")

if (${BUILD_AGAINST_PRECOMPILED_LLVM})
  set (search_paths
    ${PATH_TO_LLVM}
    ${PATH_TO_LLVM}/lib/cmake
    ${PATH_TO_LLVM}/lib/cmake/llvm
    ${PATH_TO_LLVM}/lib/cmake/clang
    ${PATH_TO_LLVM}/share/clang/cmake/
    ${PATH_TO_LLVM}/share/llvm/cmake/
  )

  find_package(LLVM REQUIRED CONFIG PATHS ${search_paths} NO_DEFAULT_PATH)
  find_package(Clang REQUIRED CONFIG PATHS ${search_paths} NO_DEFAULT_PATH)

  list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")

else()
  
  set (LLVM_ENABLE_PROJECTS "clang" CACHE BOOL "Build only Clang when building against monorepo" FORCE)
  set (LLVM_TARGETS_TO_BUILD "host" CACHE STRING "Only build targets for host architecture" FORCE)
  #set (BUILD_SHARED_LIBS "On" CACHE BOOL "Link LLVM libraries dynamically" FORCE)

  add_subdirectory(${PATH_TO_LLVM} llvm-build)

  if (NOT TARGET clangTooling)
    message(FATAL_ERROR " 
 Cannot find clangTooling target. Did you forget to clone clang sources?
 Clean CMake cache and make sure they are available at:
 ${PATH_TO_LLVM}/tools/clang")
  endif()

  # Normally, include paths provided by LLVMConfig.cmake
  # In this case we can 'steal' them from real targets
  get_target_property(llvm_support_includes LLVMSupport INCLUDE_DIRECTORIES)
  get_target_property(clang_tooling_includes clangTooling INCLUDE_DIRECTORIES)
  set(LLVM_INCLUDE_DIRS ${llvm_support_includes} ${clang_tooling_includes})
  list(REMOVE_DUPLICATES LLVM_INCLUDE_DIRS)

  # Manually include the llvm CMake modules
  list(APPEND CMAKE_MODULE_PATH
    "${PATH_TO_LLVM}/cmake"
    "${PATH_TO_LLVM}/cmake/modules"
  )

Tim Gymnich's avatar
Tim Gymnich committed
81
82
  set(LLVM_MAIN_SRC_DIR ${PATH_TO_LLVM})

Tim Gymnich's avatar
Tim Gymnich committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
endif()

include(LLVM-Config)
include(HandleLLVMOptions)
include(AddLLVM)

if ("LLVM" IN_LIST LLVM_AVAILABLE_LIBS)
  set (LLVM_AVAILABLE_LIBS
    LLVM
  )
else()
  set (LLVM_AVAILABLE_LIBS
    LLVMSupport
    LLVMCore  
    LLVMAnalysis
  )
endif()

Tim Gymnich's avatar
Tim Gymnich committed
101
set(PAIN_SOURCES
Administrator's avatar
initial  
Administrator committed
102
  src/fixpoint.cpp
Tim Gymnich's avatar
Tim Gymnich committed
103
  src/fixpoint_widening.cpp
Administrator's avatar
initial  
Administrator committed
104
105
  src/value_set.cpp
  src/simple_interval.cpp
Tim Gymnich's avatar
Tim Gymnich committed
106
  src/normalized_conjunction.cpp
Tim Gymnich's avatar
Tim Gymnich committed
107
  src/linear_equality.cpp
Tim Gymnich's avatar
Tim Gymnich committed
108
  src/linear_subspace.cpp
Tim Gymnich's avatar
Tim Gymnich committed
109
110
)

Tim Gymnich's avatar
Tim Gymnich committed
111
set(PAIN_HEADERS
Tim Gymnich's avatar
Tim Gymnich committed
112
113
114
  src/fixpoint.h
  src/value_set.h
  src/simple_interval.h
Tim Gymnich's avatar
Tim Gymnich committed
115
  src/normalized_conjunction.h
Tim Gymnich's avatar
Tim Gymnich committed
116
  src/linear_equality.h
Tim Gymnich's avatar
Tim Gymnich committed
117
118
  src/general.h
  src/global.h
119
  src/abstract_state.h
Tim Gymnich's avatar
Tim Gymnich committed
120
  src/hash_utils.h
Tim Gymnich's avatar
Tim Gymnich committed
121
122
123
  src/linear_subspace.h
  src/simple_matrix.h
  src/sparse_matrix.h
Tim Gymnich's avatar
Tim Gymnich committed
124
125
)

Tim Gymnich's avatar
Tim Gymnich committed
126
127
include_directories(${LLVM_INCLUDE_DIRS})

Tim Gymnich's avatar
Tim Gymnich committed
128
add_llvm_library(llvm-pain MODULE
Tim Gymnich's avatar
Tim Gymnich committed
129
130
  ${PAIN_SOURCES}
  ${PAIN_HEADERS}
Administrator's avatar
initial  
Administrator committed
131
132
  DEPENDS
  intrinsics_gen
Tim Gymnich's avatar
Tim Gymnich committed
133
  irgen
Administrator's avatar
initial  
Administrator committed
134
135
  PLUGIN_TOOL
  opt
Tim Gymnich's avatar
Tim Gymnich committed
136
137
)

Tim Gymnich's avatar
Tim Gymnich committed
138
139
140
141
#
# Tests
#

Tim Gymnich's avatar
Tim Gymnich committed
142
143
add_llvm_executable(simple_interval_test
   test/simple_interval_test.cpp
Tim Gymnich's avatar
Tim Gymnich committed
144
145
   ${PAIN_HEADERS}
   ${PAIN_SOURCES}
Tim Gymnich's avatar
Tim Gymnich committed
146
   DEPENDS
Tim Gymnich's avatar
Tim Gymnich committed
147
   irgen
Tim Gymnich's avatar
Tim Gymnich committed
148
149
150
151
   intrinsics_gen
)

target_link_libraries(simple_interval_test
152
   PRIVATE ${LLVM_AVAILABLE_LIBS}
Tim Gymnich's avatar
Tim Gymnich committed
153
154
)

Tim Gymnich's avatar
Tim Gymnich committed
155
156
add_llvm_executable(normalized_conjunction_test
   test/normalized_conjunction_test.cpp
Tim Gymnich's avatar
Tim Gymnich committed
157
158
   ${PAIN_HEADERS}
   ${PAIN_SOURCES}
Tim Gymnich's avatar
Tim Gymnich committed
159
   DEPENDS
Tim Gymnich's avatar
Tim Gymnich committed
160
   irgen
Tim Gymnich's avatar
Tim Gymnich committed
161
162
163
164
   intrinsics_gen
)

target_link_libraries(normalized_conjunction_test
165
   PRIVATE ${LLVM_AVAILABLE_LIBS}
Tim Gymnich's avatar
Tim Gymnich committed
166
167
)

Tim Gymnich's avatar
Tim Gymnich committed
168
169
add_llvm_executable(linear_subspace_test
   test/linear_subspace_test.cpp
Tim Gymnich's avatar
Tim Gymnich committed
170
171
172
173
174
175
176
   ${PAIN_HEADERS}
   ${PAIN_SOURCES}
   DEPENDS
   irgen
   intrinsics_gen
)

Tim Gymnich's avatar
Tim Gymnich committed
177
target_link_libraries(linear_subspace_test
178
   PRIVATE ${LLVM_AVAILABLE_LIBS}
Tim Gymnich's avatar
Tim Gymnich committed
179
180
)

Tim Gymnich's avatar
Tim Gymnich committed
181
182
add_llvm_executable(simple_matrix_test
   test/simple_matrix_test.cpp
Tim Gymnich's avatar
Tim Gymnich committed
183
184
185
186
   ${PAIN_HEADERS}
   ${PAIN_SOURCES}
)

Tim Gymnich's avatar
Tim Gymnich committed
187
target_link_libraries(simple_matrix_test
188
   PRIVATE ${LLVM_AVAILABLE_LIBS}
Tim Gymnich's avatar
Tim Gymnich committed
189
190
191
192
193
194
195
196
197
)

add_llvm_executable(sparse_matrix_test
   test/sparse_matrix_test.cpp
   ${PAIN_HEADERS}
   ${PAIN_SOURCES}
)

target_link_libraries(sparse_matrix_test
198
   PRIVATE ${LLVM_AVAILABLE_LIBS}
Tim Gymnich's avatar
Tim Gymnich committed
199
200
)

Tim Gymnich's avatar
Tim Gymnich committed
201
202
203
enable_testing()

add_test(NAME intervalAnalysisTest
Tim Gymnich's avatar
Tim Gymnich committed
204
  COMMAND opt --load $<TARGET_FILE:llvm-pain> --painpass -S ${CMAKE_SOURCE_DIR}/output/add-1.ll
Tim Gymnich's avatar
Tim Gymnich committed
205
206
207
208
)

add_test(NAME simpleIntervalTest
   COMMAND simple_interval_test
Tim Gymnich's avatar
Tim Gymnich committed
209
210
211
212
)

add_test(NAME normalizedConjunctionTest
   COMMAND normalized_conjunction_test
213
)
214

Tim Gymnich's avatar
Tim Gymnich committed
215
add_test(NAME linearSubspaceTest
Tim Gymnich's avatar
Tim Gymnich committed
216
   COMMAND linear_subspace_test
Tim Gymnich's avatar
Tim Gymnich committed
217
218
)

Tim Gymnich's avatar
Tim Gymnich committed
219
220
add_test(NAME simpleMatrixTest
   COMMAND simple_matrix_test
Tim Gymnich's avatar
Tim Gymnich committed
221
222
)

Tim Gymnich's avatar
Tim Gymnich committed
223
224
225
226
227
228
229
230
add_test(NAME sparseMatrixTest
   COMMAND sparse_matrix_test
)

#
# Samples
#

231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
set(SAMPLES
  add-1-float
  add-1
  add-2
  add-3
  add-4
  basic_function
  branching
  cmp-two-variables-1
  cmp-two-variables-2
  euler-48
  euler
  example
  for-loop-1
  for
  func-test-1
  func-test-2
  func-test-3
  func-test-4
  func-test-5
  func-test-for
  func-test-rec-endless
  func-test-rec
  func-test-switch
  gcd
  goto
  if-and
  if-multiple-of-4
  if-then-else-2
  if-then-else-complicated
  if-then-else-two-constraints
  if-then-else
  multiple-functions
  ops
  switch-2
  switch-3
  switch-two-labels
  switch
  while-1
  while-2
  while-bigger-steps
  while-neg
Tim Gymnich's avatar
Tim Gymnich committed
273
  example-1
274
275
276
277
278
279
280
#  A
#  B
#  C
#  D
)
set(CPPSAMPLES
  classes
281
282
)

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Older CMake version do not support list transformations
macro(list_transform_prepend in prefix)
    foreach(f ${${in}})
        list(APPEND temp "${prefix}${f}")
    endforeach()
    set(${in} "${temp}")
    unset(temp)
endmacro()

macro(list_transform_append in suffix out)
    foreach(f ${${in}})
        list(APPEND ${out} "${f}${suffix}")
    endforeach()
endmacro()

list_transform_append(SAMPLES ".c" SAMPLES_C)
list_transform_append(SAMPLES ".bc" SAMPLES_BC)
list_transform_append(SAMPLES ".ll" SAMPLES_LL)
list_transform_prepend(SAMPLES_LL "${CMAKE_SOURCE_DIR}/output/")
list_transform_prepend(SAMPLES_BC "${CMAKE_SOURCE_DIR}/output/")
303
304
305
306
307
308
309
list_transform_prepend(SAMPLES_C "${CMAKE_SOURCE_DIR}/samples/")
list_transform_append(CPPSAMPLES      ".cpp" CPPSAMPLES_CPP)
list_transform_append(CPPSAMPLES      ".bc"  CPPSAMPLES_BC)
list_transform_append(CPPSAMPLES      ".ll"  CPPSAMPLES_LL)
list_transform_prepend(CPPSAMPLES_LL "${CMAKE_SOURCE_DIR}/output/")
list_transform_prepend(CPPSAMPLES_BC "${CMAKE_SOURCE_DIR}/output/")
list_transform_prepend(CPPSAMPLES_CPP "${CMAKE_SOURCE_DIR}/samples/")
Tim Gymnich's avatar
Tim Gymnich committed
310
311

add_custom_target(irgen
312
313
  DEPENDS ${SAMPLES_LL} ${SAMPLES_BC} ${CPPSAMPLES_LL} ${CPPSAMPLES_BC}
 )
Tim Gymnich's avatar
Tim Gymnich committed
314

315
316
317
318
319
320
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/output)

if (APPLE)

foreach(src ${SAMPLES})
  add_custom_command(
Tim Gymnich's avatar
Tim Gymnich committed
321
    OUTPUT ${CMAKE_SOURCE_DIR}/output/${src}.ll
322
    COMMAND ${PATH_TO_LLVM}/bin/clang --sysroot ${CMAKE_OSX_SYSROOT} -O0 -fno-discard-value-names -emit-llvm ${CMAKE_SOURCE_DIR}/samples/${src}.c -Xclang -disable-O0-optnone -c -o ${CMAKE_SOURCE_DIR}/output/${src}.bc
323
    COMMAND ${PATH_TO_LLVM}/bin/opt -S -mem2reg ${CMAKE_SOURCE_DIR}/output/${src}.bc -o ${CMAKE_SOURCE_DIR}/output/${src}.ll
Tim Gymnich's avatar
Tim Gymnich committed
324
    DEPENDS clang opt ${CMAKE_SOURCE_DIR}/samples/${src}.c
325
326
327
328
329
330
331
    COMMENT "Generating LLVM IR for example ${src}.c"
  )
endforeach(src)

foreach(src ${CPPSAMPLES})
  add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/output/${src}.ll
332
    COMMAND ${PATH_TO_LLVM}/bin/clang++ --sysroot ${CMAKE_OSX_SYSROOT} -O0  -fno-discard-value-names  -emit-llvm ${CMAKE_SOURCE_DIR}/samples/${src}.cpp -Xclang -disable-O0-optnone -c -o ${CMAKE_SOURCE_DIR}/output/${src}.bc
333
    COMMAND ${PATH_TO_LLVM}/bin/opt -S -mem2reg ${CMAKE_SOURCE_DIR}/output/${src}.bc -o ${CMAKE_SOURCE_DIR}/output/${src}.ll
334
335
    DEPENDS clang opt ${CMAKE_SOURCE_DIR}/samples/${src}.cpp
    COMMENT "Generating LLVM IR for example ${src}.cpp"
336
337
338
339
340
341
342
  )
endforeach(src)

else()

foreach(src ${SAMPLES})
  add_custom_command(
Tim Gymnich's avatar
Tim Gymnich committed
343
    OUTPUT ${CMAKE_SOURCE_DIR}/output/${src}.ll
344
    COMMAND ${PATH_TO_LLVM}/bin/clang -O0  -fno-discard-value-names -emit-llvm ${CMAKE_SOURCE_DIR}/samples/${src}.c -Xclang -disable-O0-optnone -c -o ${CMAKE_SOURCE_DIR}/output/${src}.bc
345
    COMMAND ${PATH_TO_LLVM}/bin/opt -S -mem2reg ${CMAKE_SOURCE_DIR}/output/${src}.bc -o ${CMAKE_SOURCE_DIR}/output/${src}.ll
Tim Gymnich's avatar
Tim Gymnich committed
346
    DEPENDS clang opt ${CMAKE_SOURCE_DIR}/samples/${src}.c
347
    COMMENT "Generating LLVM IR for example ${src}.c"
348
349
350
  )
endforeach(src)

351
352
353
foreach(src ${CPPSAMPLES})
  add_custom_command(
    OUTPUT ${CMAKE_SOURCE_DIR}/output/${src}.ll
354
    COMMAND ${PATH_TO_LLVM}/bin/clang++ -O0  -fno-discard-value-names -emit-llvm ${CMAKE_SOURCE_DIR}/samples/${src}.cpp -Xclang -disable-O0-optnone -c -o ${CMAKE_SOURCE_DIR}/output/${src}.bc
355
    COMMAND ${PATH_TO_LLVM}/bin/opt -S -mem2reg ${CMAKE_SOURCE_DIR}/output/${src}.bc -o ${CMAKE_SOURCE_DIR}/output/${src}.ll
356
    COMMAND optdir=${PATH_TO_LLVM}/bin ${CMAKE_SOURCE_DIR}/helpers/lsFunctions.sh ${CMAKE_SOURCE_DIR}/output/${src}.ll
Administrator's avatar
Administrator committed
357
    COMMAND optdir=${PATH_TO_LLVM}/bin ${CMAKE_SOURCE_DIR}/helpers/callgraph.sh ${CMAKE_SOURCE_DIR}/output/${src}.ll
358
359
360
361
362
363
    DEPENDS clang opt ${CMAKE_SOURCE_DIR}/samples/${src}.cpp
    COMMENT "Generating LLVM IR for example ${src}.cpp"
  )

endforeach(src)

364
endif()
365
366

configure_file(.lldbinit.in .lldbinit)