Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize CodeGen for the executable JAR #43560

Open
wants to merge 200 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
200 commits
Select commit Hold shift + click to select a range
bd8f4fd
Add DeadCodeAnalyzer
Thushara-Piyasekara Aug 6, 2023
0c05d02
Add dead code detection for functions and anonymous functions
Thushara-Piyasekara Aug 9, 2023
1d7a670
Add comments and optimizations
Thushara-Piyasekara Aug 9, 2023
00c1143
Add Dead Code Detection for multiple modules
Thushara-Piyasekara Aug 17, 2023
da0a68b
Add function invocation persistence through modifying BIR
Thushara-Piyasekara Sep 13, 2023
508e7de
Add deletion of unused top level functions from thin jars
Thushara-Piyasekara Sep 22, 2023
71a5b06
Remove ModuleData class. Optimize DeadCodeAnalyzer
Thushara-Piyasekara Sep 26, 2023
be5fb8b
Add comments. Fix certain langlib children not being updated
Thushara-Piyasekara Sep 26, 2023
2131060
Add file license signatures and optimizations
Thushara-Piyasekara Sep 26, 2023
aad61ff
Minor optimizations
Thushara-Piyasekara Sep 27, 2023
c48745b
Fix certain child functions not being added to parent symbol
Thushara-Piyasekara Sep 27, 2023
a32c3f9
Fix certain child functions not being added to parent symbol
Thushara-Piyasekara Sep 27, 2023
cdf3030
Optimize code according to best practices and suggestions
Thushara-Piyasekara Oct 2, 2023
498e552
Refactor CompilerPhaseRunner
Thushara-Piyasekara Oct 3, 2023
4fa6129
Improve Resource function handling logic
Thushara-Piyasekara Oct 3, 2023
85a16bf
Refactor licence and variable declarations
Thushara-Piyasekara Oct 5, 2023
2bb88b1
Refactor file separator
Thushara-Piyasekara Oct 5, 2023
2f57ba8
Add BIRDeadNodeAnalyzer
Thushara-Piyasekara Oct 12, 2023
0e17b66
Add detection and deletion of unused Type Definitions
Thushara-Piyasekara Oct 18, 2023
158fb6d
Add optimized Thin JAR object generation
Thushara-Piyasekara Oct 27, 2023
92bbf65
Add BIR TypeDefAnalyzer
Thushara-Piyasekara Oct 31, 2023
0233cfc
Add documentation for TypeDefAnalyzer
Thushara-Piyasekara Oct 31, 2023
76ee647
Add analyzer support for cyclic typeDefs and typeDef function calls
Thushara-Piyasekara Nov 1, 2023
86ff789
Refactor the analyzer algorithm
Thushara-Piyasekara Nov 15, 2023
a66b501
Add interop dependencies white listing
Thushara-Piyasekara Nov 20, 2023
8961afd
Fix TypeDefAnalyzer bugs and refactor code
Thushara-Piyasekara Dec 1, 2023
2c5dba0
Handle null pointer exceptions in BIRNode.getPackageID
Thushara-Piyasekara Dec 1, 2023
7eb8ea2
Add new entries for interop-dependencies
Thushara-Piyasekara Dec 1, 2023
e208b23
Refactor DeadBIRNodeAnalyzer
Thushara-Piyasekara Dec 1, 2023
22f97d5
Add unused module elimination
Thushara-Piyasekara Dec 20, 2023
db0d529
Add compilation support for langlibs
Thushara-Piyasekara Dec 20, 2023
cea07f9
Add RecordDefaultFPLoad instruction optimization support
Thushara-Piyasekara Jan 8, 2024
6a12e4b
Add native bytecode optimizer
Thushara-Piyasekara Jan 22, 2024
365ca30
Revert langlib compilation
Thushara-Piyasekara Jan 23, 2024
64a575f
Revert to original PackageID assignment
Thushara-Piyasekara Feb 1, 2024
31b4738
Add interop dependencies for ballerina/sql library
Thushara-Piyasekara Feb 2, 2024
1873832
Add new CLI output for optimized build
Thushara-Piyasekara Feb 2, 2024
d63adae
Add condition check for optimized build
Thushara-Piyasekara Feb 2, 2024
b108551
Refactor deprecated analyzer names
Thushara-Piyasekara Feb 2, 2024
5e8128f
Refactor license signatures for newly added files
Thushara-Piyasekara Feb 2, 2024
cd03193
Add null check for markSelfAndChildrenAsUsed function
Thushara-Piyasekara Feb 6, 2024
5a7d458
Add logic for handling whitelisted packages
Thushara-Piyasekara Feb 6, 2024
3c84dad
Refactor LargeMethodOptimizer
Thushara-Piyasekara Feb 6, 2024
36412a6
Add missed attribute analyzing for BStreamType
Thushara-Piyasekara Feb 6, 2024
66eba59
Update interop-dependency version
Thushara-Piyasekara Feb 6, 2024
ef26f9e
Add duration reports for codegen optimization
Thushara-Piyasekara Feb 6, 2024
e0df326
Refactor JBallerinaBackend
Thushara-Piyasekara Feb 6, 2024
186f8ef
Add reflection and driver class whitelisting
Thushara-Piyasekara Feb 6, 2024
39c1941
Add reflection handling for NativeDependencyOptimizer
Thushara-Piyasekara Feb 6, 2024
176d18c
Improve logic for function pointer handling
Thushara-Piyasekara Feb 6, 2024
2e84fc9
Whitelist bal files with interop dependency types
Thushara-Piyasekara Feb 6, 2024
57b71d3
Refactor UsedTypeDefAnalyzer function names
Thushara-Piyasekara Feb 6, 2024
df99d60
Remove deprecated DCE algorithms
Thushara-Piyasekara Feb 6, 2024
a11435c
Refactor JBallerinaBackend
Thushara-Piyasekara Feb 7, 2024
e5c2ae1
Update interop-dependencies.properties
Thushara-Piyasekara Feb 7, 2024
2a521ed
Refactor NativeDependencyOptimizer
Thushara-Piyasekara Feb 8, 2024
af062aa
Remove unused code in JBallerinaBackend
Thushara-Piyasekara Feb 8, 2024
833090f
Remove unused code
Thushara-Piyasekara Feb 8, 2024
1d01b6a
Remove unused code
Thushara-Piyasekara Feb 8, 2024
d998814
Remove unused code and files
Thushara-Piyasekara Feb 8, 2024
a122295
Refactor logic for UsedBIRNodeAnalyzer and UsedTypeDefAnalyzer
Thushara-Piyasekara Feb 9, 2024
d53367c
Refactor high level logic for CodeGen optimization
Thushara-Piyasekara Feb 14, 2024
1ecd1f8
Update interop-dependencies for latest http package
Thushara-Piyasekara Feb 16, 2024
714469b
Change builtin PackageID with parent PackageId
Thushara-Piyasekara Feb 16, 2024
450edff
Add analyzer support for table type
Thushara-Piyasekara Feb 17, 2024
a9b9ef8
Add fix for handling multiple reference types for the same parent type
Thushara-Piyasekara Feb 17, 2024
0ec90c6
Refactor duplicate code into single functions
Thushara-Piyasekara Feb 20, 2024
38a8c60
Fix review suggestions
Thushara-Piyasekara Feb 20, 2024
1afc0ee
Refactor ClassNodeVisitor
Thushara-Piyasekara Feb 20, 2024
3c9320b
Change bir optimized jar file name
Thushara-Piyasekara Feb 20, 2024
2c46493
Fix record default fields containing function pointers not being dete…
Thushara-Piyasekara Feb 20, 2024
c547991
Restore max memory heap size to original
Thushara-Piyasekara Feb 22, 2024
2ecffea
Refactor according to review suggestions
Thushara-Piyasekara Feb 26, 2024
9921459
Add optimization support for testablePkgs
Thushara-Piyasekara Feb 27, 2024
d53f6bc
Add bir optimization support for test command
Thushara-Piyasekara Mar 4, 2024
9e5a1e1
Fix testablePkg birFunctions not being analyzed
Thushara-Piyasekara Mar 5, 2024
b5e0940
Refactor optimized test CLI messages
Thushara-Piyasekara Mar 5, 2024
dd791b0
Fix mock functions being labeled as UNUSED
Thushara-Piyasekara Mar 6, 2024
897a597
Add duplicate thin jar generation for testing
Thushara-Piyasekara Mar 14, 2024
a1479dc
Add working version of the duplicate codegen
Thushara-Piyasekara Apr 3, 2024
b92163c
Refactor _DUPLICATE prefix with _OPTIMIZED
Thushara-Piyasekara Apr 9, 2024
34b799f
Refactor logic for adding optimized library paths
Thushara-Piyasekara Apr 9, 2024
f647379
Refactor immutable type optimization
Thushara-Piyasekara Apr 11, 2024
80e4836
Fix checkStyle violations
Thushara-Piyasekara Apr 13, 2024
711d93c
Add json report generation for testing
Thushara-Piyasekara Apr 16, 2024
28e6f0b
Fix function pointer types not being analyzed
Thushara-Piyasekara Apr 17, 2024
dfd518e
Add bytecode optimization for single file projects
Thushara-Piyasekara Apr 17, 2024
e54e2f4
Fix error types being marked as unused
Thushara-Piyasekara Apr 17, 2024
46c0d87
Add SingleFileProject tests
Thushara-Piyasekara Apr 19, 2024
329149b
Fix FPs not being marked as used in different bbs
Thushara-Piyasekara Apr 22, 2024
223d5fa
Fix unused submodules not being optimized
Thushara-Piyasekara Apr 24, 2024
2549c0e
Add new SingleFileProject test cases
Thushara-Piyasekara Apr 24, 2024
cca665c
Add initial build project tests
Thushara-Piyasekara May 6, 2024
fe4836e
Refactor test resources directory structure
Thushara-Piyasekara May 7, 2024
7616c6d
Add verbose flag for report generation
Thushara-Piyasekara May 7, 2024
e5cfd67
Add NativeDependencyOptimizationReport generation
Thushara-Piyasekara May 7, 2024
71aa482
Add build project test cases
Thushara-Piyasekara May 8, 2024
d392fed
Add initial tests for bytecode optimizer
Thushara-Piyasekara May 10, 2024
ecdb0d6
Add class compilation for test project native libs
Thushara-Piyasekara May 13, 2024
55435a8
Add new tests
Thushara-Piyasekara May 13, 2024
5e5958b
Add worker related unit tests
Thushara-Piyasekara May 14, 2024
511628b
Add client and listener related unit tests
Thushara-Piyasekara May 15, 2024
b286bdd
Add anonymous function usage tests
Thushara-Piyasekara May 15, 2024
0f19142
Add licencing headers for tests
Thushara-Piyasekara May 16, 2024
d7cec38
Fix wrong package name in unit test expected json
Thushara-Piyasekara May 22, 2024
2771263
Add ExternalDependency white listing
Thushara-Piyasekara May 22, 2024
c5c9eb8
Add null check for annotation check
Thushara-Piyasekara May 27, 2024
4564126
Fix merge conflicts
Thushara-Piyasekara May 27, 2024
ff0b3e1
Add version independent typedef whitelisting
Thushara-Piyasekara May 27, 2024
a1ff52c
Fix checkstyle violations
Thushara-Piyasekara May 28, 2024
05afd23
Fix getJarFilePathsRequiredForExecution() overload
Thushara-Piyasekara May 28, 2024
2dc6048
Fix spotbugs violations
Thushara-Piyasekara May 28, 2024
3b1cbe2
Fix spotbugs violations
Thushara-Piyasekara May 28, 2024
6c392c4
Add temporary fix for static field value retention
Thushara-Piyasekara May 29, 2024
4d79a3d
Add string constants for optimized jar identifier
Thushara-Piyasekara May 31, 2024
dc9232b
Refactor common dependency identification logic
Thushara-Piyasekara May 31, 2024
5512265
Fix CNF error for optimized testing
Thushara-Piyasekara Jun 2, 2024
d4e9806
Fix testable pkg dependencies being optimized
Thushara-Piyasekara Jun 2, 2024
062afc3
Fix optimized jars not being added to classpath
Thushara-Piyasekara Jun 3, 2024
1f5e949
Add temporary fix for test imports being optimized
Thushara-Piyasekara Jun 3, 2024
aaebcad
Fix checkstyle violations
Thushara-Piyasekara Jun 3, 2024
2ef9677
Fix spotbugs violations
Thushara-Piyasekara Jun 3, 2024
aca840d
Fix spotbugs violations
Thushara-Piyasekara Jun 3, 2024
285414a
Add missing headers and javadoc comments
Thushara-Piyasekara Jun 3, 2024
3ec12f8
Refactor according to review suggestions
Thushara-Piyasekara Jun 4, 2024
bd79abf
Update licensing and javadocs
Thushara-Piyasekara Jun 4, 2024
6c1c554
Fix testable pkg analysis affecting optimized JAR
Thushara-Piyasekara Jun 5, 2024
b7b0efa
Add constants for String references
Thushara-Piyasekara Jun 5, 2024
0c76930
Fix review suggestions
Thushara-Piyasekara Jun 6, 2024
9f21994
Fix review suggestions
Thushara-Piyasekara Jun 6, 2024
67a20b2
Fix review suggestions
Thushara-Piyasekara Jun 6, 2024
3a9b728
Fix imported global vars not being marked as used
Thushara-Piyasekara Jun 7, 2024
a3e3fab
Fix review suggestions
Thushara-Piyasekara Jun 7, 2024
4288887
Fix merge conflicts
Thushara-Piyasekara Jun 7, 2024
3881f06
Fix CheckStyle violations
Thushara-Piyasekara Jun 7, 2024
49f220d
Fix NativeDependencyOptimizationTests failure
Thushara-Piyasekara Jun 10, 2024
ba343d3
Fix NativeDependencyOptimizationTests failure
Thushara-Piyasekara Jun 11, 2024
fd0f93c
Fix checkstyle violations
Thushara-Piyasekara Jun 11, 2024
b530f9b
Fix review suggestions
Thushara-Piyasekara Jun 11, 2024
501bd6d
Add --verbose flag usage validation
Thushara-Piyasekara Jun 12, 2024
301ac60
Add jacoco for NativeDependencyOptimizationTests
Thushara-Piyasekara Jun 12, 2024
9197803
Remove CLI optimization report emitter
Thushara-Piyasekara Jun 13, 2024
8d43276
Add tests for optimized test suite execution
Thushara-Piyasekara Jun 13, 2024
16330a8
Add unix output for OptimizedExecutableTestingTest
Thushara-Piyasekara Jun 14, 2024
d30198a
Refactor "verbose" flag to "optimize-report"
Thushara-Piyasekara Jun 14, 2024
7b2cb89
Fix review suggestions
Thushara-Piyasekara Jun 14, 2024
8f558fa
Add conditional init for optimizer related fields
Thushara-Piyasekara Jun 14, 2024
330312f
Add optimize flag validation for JarResolver
Thushara-Piyasekara Jun 14, 2024
717f182
Add optimize flag validation for jar packing
Thushara-Piyasekara Jun 14, 2024
99c0b8f
Refactor common dependency marking logic for tests
Thushara-Piyasekara Jun 17, 2024
b7f8630
Refactor unusedPackageID variable names
Thushara-Piyasekara Jun 17, 2024
2c9b400
Make CodeGenOptimizationReportEmitter singleton
Thushara-Piyasekara Jun 17, 2024
c45e530
Refactor optimization report path resolving logic
Thushara-Piyasekara Jun 19, 2024
6b454c9
Fix review suggestions
Thushara-Piyasekara Jun 20, 2024
d8bb252
Fix review suggestions
Thushara-Piyasekara Jun 24, 2024
6aa8280
Fix review suggestions
Thushara-Piyasekara Jun 24, 2024
e1d0d7b
Move static compilation options to the builder
Thushara-Piyasekara Jun 25, 2024
2abd708
Fix review suggestions
Thushara-Piyasekara Jun 25, 2024
dc7b1ee
Use reflection to reset static fields in unit test
Thushara-Piyasekara Jun 25, 2024
c6bf269
Fix review suggestions
Thushara-Piyasekara Jun 26, 2024
e9559a5
Fix CheckStyle violations
Thushara-Piyasekara Jun 26, 2024
e75ba76
Fix used submodules being marked as unused
Thushara-Piyasekara Jun 26, 2024
c88dfec
Fix review suggestions
Thushara-Piyasekara Jun 26, 2024
a94ea8e
Fix checkStyle violations
Thushara-Piyasekara Jun 27, 2024
5b08ebb
Fix review suggestions
Thushara-Piyasekara Jun 27, 2024
9cf0057
Update to match master
heshanpadmasiri Jul 15, 2024
e5bf9de
Move all hardcoded code to single class
heshanpadmasiri Jul 25, 2024
22f56e4
Add flag to dissable hardcoded flags
heshanpadmasiri Jul 25, 2024
94fe94a
Fix to match master
heshanpadmasiri Sep 9, 2024
f94714b
Change flag
heshanpadmasiri Oct 24, 2024
f97861e
Fix dead code elimination with constrained types
MaryamZi Nov 8, 2024
d3e7bad
Fix fpLoadInstructions being deleted from test pkgs
Thushara-Piyasekara Nov 8, 2024
66779d4
Add tests
Thushara-Piyasekara Nov 8, 2024
dac6797
Update tests/testerina-integration-test/src/test/resources/codegen-op…
gimantha Nov 8, 2024
3f08de3
Update tests/testerina-integration-test/src/test/java/org/ballerinala…
gimantha Nov 8, 2024
5b88083
Update tests/testerina-integration-test/src/test/resources/project-ba…
gimantha Nov 8, 2024
f5a6f0c
Merge pull request #43559 from Thushara-Piyasekara/code-gen-optimizer…
gimantha Nov 8, 2024
001a756
Merge pull request #43557 from MaryamZi/dce
gimantha Nov 8, 2024
e75154e
Fix deletion of buildPkg functions invoked only by testablePkg
Thushara-Piyasekara Nov 8, 2024
01576b2
Merge pull request #43561 from Thushara-Piyasekara/code-gen-optimizer…
gimantha Nov 8, 2024
ce0dc94
Update integration test output for windows
Thushara-Piyasekara Nov 8, 2024
af28a18
Merge pull request #43562 from Thushara-Piyasekara/code-gen-optimizer…
MaryamZi Nov 9, 2024
21cd4ae
Fix testable pkg invocation data handling logic
Thushara-Piyasekara Nov 11, 2024
095db58
Merge pull request #43566 from Thushara-Piyasekara/code-gen-optimizer…
MaryamZi Nov 11, 2024
399303f
Fix checkstyle issue
Thushara-Piyasekara Nov 11, 2024
76598e7
Merge pull request #43567 from Thushara-Piyasekara/code-gen-optimizer…
MaryamZi Nov 11, 2024
092cc69
Merge pull request #43575 from ballerina-platform/master
MaryamZi Nov 13, 2024
38881b7
Fix optimized thin jar caching for bal test
Thushara-Piyasekara Nov 13, 2024
4227209
Fix review suggestions
Thushara-Piyasekara Nov 19, 2024
bfe8fe6
Merge pull request #43582 from Thushara-Piyasekara/code-gen-optimizer…
MaryamZi Nov 19, 2024
e8bde9a
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Nov 19, 2024
4134b08
Update workflows
MaryamZi Nov 19, 2024
f2dd1af
Merge pull request #43610 from MaryamZi/update-workflows
MaryamZi Nov 19, 2024
52ff1c6
Resolve conflicts and merge branch 'feature-codegen-optimizer' of htt…
MaryamZi Nov 19, 2024
e539bb8
Merge branch 'master' of https://github.com/ballerina-platform/baller…
MaryamZi Nov 20, 2024
f909a51
Update tests for strand annotation
MaryamZi Nov 21, 2024
98cb3c7
Fix tests
MaryamZi Nov 21, 2024
3c776af
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Nov 21, 2024
b60c011
Fix a windows test failure
MaryamZi Nov 21, 2024
b341719
Merge pull request #43609 from MaryamZi/feature-codegen-optimizer
MaryamZi Nov 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .github/workflows/pull_request_full_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
branches:
- master
- feature-codegen-optimizer

jobs:
build-lang:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pull_request_ubuntu_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ on:
- native-build
- revert-client-decl-master
- query-grouping-aggregation
- feature-codegen-optimizer

jobs:
ubuntu_build:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/pull_request_windows_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ on:
- native-build
- revert-client-decl-master
- query-grouping-aggregation
- feature-codegen-optimizer

jobs:
windows_build:
name: Build with some tests on Windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ public BuildCommand() {
@CommandLine.Option(names = "--optimize-dependency-compilation", hidden = true,
description = "experimental memory optimization for large projects")
private Boolean optimizeDependencyCompilation;
@CommandLine.Option(names = "--eliminate-dead-code", description = "eliminate dead code in generated executable",
defaultValue = "false")
private Boolean deadCodeElimination;

@CommandLine.Option(names = "--dead-code-elimination-report",
description = "generate a report containing which sections were removed as part of dead code elimination",
defaultValue = "false")
private Boolean deadCodeEliminationReport;

@Override
public void execute() {
Expand Down Expand Up @@ -291,8 +299,8 @@ public void execute() {
// resolve maven dependencies in Ballerina.toml
.addTask(new ResolveMavenDependenciesTask(outStream))
// compile the modules
.addTask(new CompileTask(outStream, errStream, false, true,
isPackageModified, buildOptions.enableCache()))
.addTask(new CompileTask(outStream, errStream, false, true, false, isPackageModified,
buildOptions.enableCache()))
.addTask(new CreateExecutableTask(outStream, this.output, null, false))
.addTask(new DumpBuildTimeTask(outStream), !project.buildOptions().dumpBuildTime())
.build();
Expand Down Expand Up @@ -326,7 +334,10 @@ private BuildOptions constructBuildOptions() {
.disableSyntaxTreeCaching(disableSyntaxTreeCaching)
.setGraalVMBuildOptions(graalVMBuildOptions)
.setShowDependencyDiagnostics(showDependencyDiagnostics)
.setOptimizeDependencyCompilation(optimizeDependencyCompilation);
.setOptimizeDependencyCompilation(optimizeDependencyCompilation)
.setShowDependencyDiagnostics(showDependencyDiagnostics)
.setEliminateDeadCode(deadCodeElimination)
.setDeadCodeEliminationReport(deadCodeEliminationReport);

if (targetDir != null) {
buildOptionsBuilder.targetDir(targetDir.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ public void execute() {
.addTask(new CleanTargetDirTask(isPackageModified, buildOptions.enableCache()), isSingleFileBuild)
.addTask(new RunBuildToolsTask(outStream), isSingleFileBuild)
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CompileTask(outStream, errStream, true, false,
isPackageModified, buildOptions.enableCache()))
.addTask(new CompileTask(outStream, errStream, true, false, false, isPackageModified,
buildOptions.enableCache()))
.addTask(new CreateBalaTask(outStream))
.addTask(new DumpBuildTimeTask(outStream), !project.buildOptions().dumpBuildTime())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private TaskExecutor createTaskExecutor(boolean isPackageModified, String[] args
.addTask(new CleanTargetDirTask(isPackageModified, buildOptions.enableCache()), isSingleFileBuild)
.addTask(new RunBuildToolsTask(outStream), isSingleFileBuild)
.addTask(new ResolveMavenDependenciesTask(outStream))
.addTask(new CompileTask(outStream, errStream, false, false, isPackageModified,
.addTask(new CompileTask(outStream, errStream, false, false, false, isPackageModified,
buildOptions.enableCache()))
.addTask(new CreateExecutableTask(outStream, null, null, false), false)
.addTask(new DumpBuildTimeTask(outStream), false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ public void execute() {
// resolve maven dependencies in Ballerina.toml
.addTask(new ResolveMavenDependenciesTask(outStream))
// compile the modules
.addTask(new CompileTask(outStream, errStream, false, false,
isPackageModified, buildOptions.enableCache()))
.addTask(new CompileTask(outStream, errStream, false, false, false, isPackageModified,
buildOptions.enableCache()))
// .addTask(new CopyResourcesTask(), isSingleFileBuild)
.addTask(new CreateExecutableTask(outStream, null, target, true))
.addTask(runExecutableTask = new RunExecutableTask(args, outStream, errStream, target))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,17 @@ public TestCommand() {
@CommandLine.Option(names = "--cloud", description = "Enable cloud artifact generation")
private String cloud;

@CommandLine.Option(names = "--eliminate-dead-code", description = "eliminate dead code in generated executable",
defaultValue = "false")
private Boolean eliminateDeadCode;

@CommandLine.Option(names = "--optimize-dependency-compilation", hidden = true,
description = "experimental memory optimization for large projects")
private Boolean optimizeDependencyCompilation;
@CommandLine.Option(names = "--dead-code-elimination-report",
description = "generate a report containing which sections were removed as part of dead code elimination",
defaultValue = "false")
private Boolean deadCodeEliminationReport;

private static final String testCmd = "bal test [--OPTIONS]\n" +
" [<ballerina-file> | <package-path>] [(-Ckey=value)...]";
Expand Down Expand Up @@ -387,8 +395,8 @@ public void execute() {
TaskExecutor taskExecutor = new TaskExecutor.TaskBuilder()
.addTask(new ResolveMavenDependenciesTask(outStream)) // resolve maven dependencies in Ballerina.toml
// compile the modules
.addTask(new CompileTask(outStream, errStream, false, false,
isPackageModified, buildOptions.enableCache()))
.addTask(new CompileTask(outStream, errStream, false, false, true, isPackageModified,
buildOptions.enableCache()))
// .addTask(new CopyResourcesTask(), listGroups) // merged with CreateJarTask
.addTask(new CreateTestExecutableTask(outStream, groupList, disableGroupList, testList, listGroups,
cliArgs, isParallelExecution),
Expand Down Expand Up @@ -429,7 +437,9 @@ private BuildOptions constructBuildOptions() {
.disableSyntaxTreeCaching(disableSyntaxTreeCaching)
.setGraalVMBuildOptions(graalVMBuildOptions)
.setShowDependencyDiagnostics(showDependencyDiagnostics)
.setOptimizeDependencyCompilation(optimizeDependencyCompilation);
.setOptimizeDependencyCompilation(optimizeDependencyCompilation)
.setEliminateDeadCode(eliminateDeadCode)
.setDeadCodeEliminationReport(deadCodeEliminationReport);

if (targetDir != null) {
buildOptionsBuilder.targetDir(targetDir.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.cli.task;

import io.ballerina.cli.utils.BuildTime;
import io.ballerina.projects.BuildOptions;
import io.ballerina.projects.CodeGeneratorResult;
import io.ballerina.projects.CodeModifierResult;
import io.ballerina.projects.JBallerinaBackend;
Expand Down Expand Up @@ -65,23 +66,26 @@ public class CompileTask implements Task {
private final transient PrintStream err;
private final boolean compileForBalPack;
private final boolean compileForBalBuild;
private final boolean compileForBalTest;
private final boolean isPackageModified;
private final boolean cachesEnabled;

public CompileTask(PrintStream out, PrintStream err) {
this(out, err, false, false, true, false);
this(out, err, false, false, false, true, false);
}

public CompileTask(PrintStream out,
PrintStream err,
boolean compileForBalPack,
boolean compileForBalBuild,
boolean compileForBalTest,
boolean isPackageModified,
boolean cachesEnabled) {
this.out = out;
this.err = err;
this.compileForBalPack = compileForBalPack;
this.compileForBalBuild = compileForBalBuild;
this.compileForBalTest = compileForBalTest;
this.isPackageModified = isPackageModified;
this.cachesEnabled = cachesEnabled;
}
Expand Down Expand Up @@ -113,6 +117,10 @@ public void execute(Project project) {
if (this.compileForBalBuild) {
addDiagnosticForProvidedPlatformLibs(project, diagnostics);
}
if (this.compileForBalBuild || this.compileForBalTest) {
addDiagnosticForInvalidDeadCodeEliminationReportFlagUsage(project, diagnostics);
}

long start = 0;

if (project.currentPackage().compilationOptions().dumpGraph()
Expand Down Expand Up @@ -336,6 +344,19 @@ private void addDiagnosticForProvidedPlatformLibs(Project project, List<Diagnost
}
}

private void addDiagnosticForInvalidDeadCodeEliminationReportFlagUsage(Project project,
List<Diagnostic> diagnostics) {
BuildOptions buildOptions = project.buildOptions();
if (buildOptions.deadCodeEliminationReport() && !buildOptions.eliminateDeadCode()) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(
ProjectDiagnosticErrorCode.INVALID_VERBOSE_FLAG_USAGE.diagnosticId(),
"--dead-code-elimination-report flag can only be used with --eliminate-dead-code flag",
DiagnosticSeverity.ERROR);
diagnostics.add(new PackageDiagnostic(diagnosticInfo,
project.currentPackage().descriptor().name().toString()));
}
}

/**
* If CompileTask is triggered by `bal pack` command, and project does not have CompilerPlugin.toml or BalTool.toml,
* skip the compilation if project is empty. The project should be evaluated for emptiness before calling this.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
import static io.ballerina.cli.utils.FileUtils.getFileNameWithoutExtension;
import static io.ballerina.projects.util.ProjectConstants.BLANG_COMPILED_JAR_EXT;
import static io.ballerina.projects.util.ProjectConstants.BYTECODE_OPTIMIZED_JAR_SUFFIX;
import static io.ballerina.projects.util.ProjectConstants.USER_DIR;

/**
Expand Down Expand Up @@ -92,6 +93,8 @@ public void execute(Project project) {
out.println(warnings);
}
emitResult = jBallerinaBackend.emit(JBallerinaBackend.OutputType.GRAAL_EXEC, executablePath);
} else if (project.buildOptions().eliminateDeadCode()) {
emitResult = jBallerinaBackend.emit(JBallerinaBackend.OutputType.OPTIMIZE_CODEGEN, executablePath);
} else {
emitResult = jBallerinaBackend.emit(JBallerinaBackend.OutputType.EXEC, executablePath);
}
Expand All @@ -118,7 +121,7 @@ public void execute(Project project) {
throw createLauncherException(e.getMessage());
}

if (!project.buildOptions().nativeImage() && !isHideTaskOutput) {
if (!project.buildOptions().nativeImage() && !isHideTaskOutput && !project.buildOptions().eliminateDeadCode()) {
Path relativePathToExecutable = currentDir.relativize(executablePath);

if (project.buildOptions().getTargetPath() != null) {
Expand All @@ -133,6 +136,24 @@ public void execute(Project project) {
}
}

// TODO: this has lot of common code with default case, refactor to common method
if (project.buildOptions().eliminateDeadCode()) {
Path relativePathToExecutable = currentDir.relativize(executablePath);
String relativePathToExecutableString =
relativePathToExecutable.toString().replace(BLANG_COMPILED_JAR_EXT, BYTECODE_OPTIMIZED_JAR_SUFFIX);
String executablePathString =
executablePath.toString().replace(BLANG_COMPILED_JAR_EXT, BYTECODE_OPTIMIZED_JAR_SUFFIX);

if (project.buildOptions().getTargetPath() != null) {
this.out.println("\t" + relativePathToExecutableString);
} else if (relativePathToExecutableString.contains("..")
|| relativePathToExecutableString.contains("." + File.separator)) {
this.out.println("\t" + executablePathString);
} else {
this.out.println("\t" + relativePathToExecutableString);
}
}

// notify plugin
// todo following call has to be refactored after introducing new plugin architecture
BuildUtils.notifyPlugins(project, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.StringJoiner;
import java.util.function.Predicate;

import static io.ballerina.cli.launcher.LauncherUtils.createLauncherException;
import static org.ballerinalang.test.runtime.util.TesterinaConstants.COVERAGE_DIR;
Expand Down Expand Up @@ -483,8 +484,10 @@ public static String getClassPath(JBallerinaBackend jBallerinaBackend, Package c
JarResolver jarResolver = jBallerinaBackend.jarResolver();
Set<Path> jars = new HashSet<>(getModuleJarPaths(jBallerinaBackend, currentPackage));

List<Path> dependencies = getTestDependencyPaths(currentPackage, jarResolver)
.stream().filter(dependency -> !jars.contains(dependency)).toList();
List<Path> dependencies = getTestDependencyPaths(currentPackage, jarResolver).stream()
.filter(Predicate.not(jars::contains))
.filter(Predicate.not(jarResolver.getOptimizedJarLibraryPaths()::contains))
.toList();

StringJoiner classPath = joinClassPaths(dependencies);
return classPath.toString();
Expand Down
2 changes: 2 additions & 0 deletions compiler/ballerina-lang/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
}
implementation libs.netty.buffer
implementation libs.ow2.asm
implementation libs.ow2.asm.tree
implementation libs.commons.io
implementation libs.zafarkhaja.jsemver
testImplementation(libs.guru.nidi.graphviz) {
Expand Down Expand Up @@ -76,6 +77,7 @@ processResources {
include 'ballerina-toml-schema.json'
include 'compiler.properties'
include 'dependencies-toml-schema.json'
include 'interop-dependencies.properties'
include 'old-dependencies-toml-schema.json'
include 'settings-toml-schema.json'
include 'bal-tools-toml-schema.json'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ public boolean showDependencyDiagnostics() {
return toBooleanDefaultIfNull(this.showDependencyDiagnostics);
}

public boolean eliminateDeadCode() {
return this.compilationOptions.eliminateDeadCode();
}

public boolean deadCodeEliminationReport() {
return this.compilationOptions.deadCodeEliminationReport();
}

/**
* Merge the given build options by favoring theirs if there are conflicts.
*
Expand Down Expand Up @@ -211,6 +219,8 @@ public BuildOptions acceptTheirs(BuildOptions theirOptions) {
buildOptionsBuilder.setExportOpenAPI(compilationOptions.exportOpenAPI);
buildOptionsBuilder.setExportComponentModel(compilationOptions.exportComponentModel);
buildOptionsBuilder.setEnableCache(compilationOptions.enableCache);
buildOptionsBuilder.setEliminateDeadCode(compilationOptions.eliminateDeadCode);
buildOptionsBuilder.setDeadCodeEliminationReport(compilationOptions.deadCodeEliminationReport);
buildOptionsBuilder.setRemoteManagement(compilationOptions.remoteManagement);
buildOptionsBuilder.setOptimizeDependencyCompilation(compilationOptions.optimizeDependencyCompilation);

Expand Down Expand Up @@ -252,7 +262,9 @@ public enum OptionName {
EXPORT_COMPONENT_MODEL("exportComponentModel"),
GRAAL_VM_BUILD_OPTIONS("graalvmBuildOptions"),
SHOW_DEPENDENCY_DIAGNOSTICS("showDependencyDiagnostics"),
OPTIMIZE_DEPENDENCY_COMPILATION("optimizeDependencyCompilation");
OPTIMIZE_DEPENDENCY_COMPILATION("optimizeDependencyCompilation"),
ELIMINATE_DEAD_CODE("eliminateDeadCode"),
DEAD_CODE_ELIMINATION_REPORT("deadCodeEliminationReport");

private final String name;

Expand Down Expand Up @@ -407,6 +419,11 @@ public BuildOptionsBuilder setNativeImage(Boolean value) {
return this;
}

public BuildOptionsBuilder setEliminateDeadCode(Boolean value) {
compilationOptionsBuilder.setEliminateDeadCode(value);
return this;
}

public BuildOptionsBuilder setRemoteManagement(Boolean value) {
compilationOptionsBuilder.setRemoteManagement(value);
return this;
Expand All @@ -428,6 +445,11 @@ public BuildOptionsBuilder setOptimizeDependencyCompilation(Boolean value) {
return this;
}

public BuildOptionsBuilder setDeadCodeEliminationReport(Boolean value) {
compilationOptionsBuilder.setDeadCodeEliminationReport(value);
return this;
}

public BuildOptions build() {
CompilationOptions compilationOptions = compilationOptionsBuilder.build();
return new BuildOptions(testReport, codeCoverage, dumpBuildTime, skipTests, compilationOptions,
Expand Down
Loading
Loading