Skip to content

Commit 7976cc0

Browse files
committed
Enable code analysis
Make it optional using the LIBSCRATCHCPP_ENABLE_CODE_ANALYZER option.
1 parent 81b1c57 commit 7976cc0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
99
option(LIBSCRATCHCPP_BUILD_UNIT_TESTS "Build unit tests" ON)
1010
option(LIBSCRATCHCPP_NETWORK_SUPPORT "Support for downloading projects" ON)
1111
option(LIBSCRATCHCPP_PRINT_LLVM_IR "Print LLVM IR of compiled Scratch scripts (for debugging)" OFF)
12+
option(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER "Analyze Scratch scripts to enable various optimizations" ON)
1213
option(LIBSCRATCHCPP_ENABLE_SANITIZER "Enable sanitizer to detect memory issues" OFF)
1314

1415
if (LIBSCRATCHCPP_ENABLE_SANITIZER)
@@ -121,6 +122,10 @@ if(LIBSCRATCHCPP_PRINT_LLVM_IR)
121122
target_compile_definitions(scratchcpp PRIVATE PRINT_LLVM_IR)
122123
endif()
123124

125+
if(LIBSCRATCHCPP_ENABLE_CODE_ANALYZER)
126+
target_compile_definitions(scratchcpp PRIVATE ENABLE_CODE_ANALYZER)
127+
endif()
128+
124129
# Macros
125130
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_LIBRARY)
126131
target_compile_definitions(scratchcpp PRIVATE LIBSCRATCHCPP_VERSION="${PROJECT_VERSION}")

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::build()
5252
m_warp = true;
5353
}
5454

55+
if (m_warp) {
56+
#ifdef ENABLE_CODE_ANALYZER
57+
// Analyze the script (type analysis, optimizations, etc.)
58+
// NOTE: Do this only for non-warp scripts
59+
m_codeAnalyzer.analyzeScript(m_instructions);
60+
#endif
61+
}
62+
5563
// Set fast math flags
5664
llvm::FastMathFlags fmf;
5765
fmf.setFast(true);

src/engine/internal/llvm/llvmcodebuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "../icodebuilder.h"
1313
#include "llvmbuildutils.h"
14+
#include "llvmcodeanalyzer.h"
1415
#include "llvminstruction.h"
1516
#include "llvminstructionlist.h"
1617
#include "llvmcoroutine.h"
@@ -137,6 +138,7 @@ class LLVMCodeBuilder : public ICodeBuilder
137138
llvm::IRBuilder<> m_builder;
138139
llvm::Function *m_function = nullptr;
139140
LLVMBuildUtils m_utils;
141+
LLVMCodeAnalyzer m_codeAnalyzer;
140142

141143
llvm::StructType *m_valueDataType = nullptr;
142144
llvm::StructType *m_stringPtrType = nullptr;

0 commit comments

Comments
 (0)