Skip to content

Commit

Permalink
Change flag
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Oct 30, 2024
1 parent 94fe94a commit f94714b
Show file tree
Hide file tree
Showing 18 changed files with 124 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +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 = "--optimize", description = "generate optimized executable jar", defaultValue = "false")
private Boolean optimizeCodegen;
@CommandLine.Option(names = "--eliminate-dead-code", description = "eliminate dead code in generated executable",
defaultValue = "false")
private Boolean deadCodeElimination;

@CommandLine.Option(names = "--optimize-report", description = "generate code generation optimization reports",
@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 optimizeReport;
private Boolean deadCodeEliminationReport;

@Override
public void execute() {
Expand Down Expand Up @@ -334,8 +336,8 @@ private BuildOptions constructBuildOptions() {
.setShowDependencyDiagnostics(showDependencyDiagnostics)
.setOptimizeDependencyCompilation(optimizeDependencyCompilation)
.setShowDependencyDiagnostics(showDependencyDiagnostics)
.setOptimizeCodegen(optimizeCodegen)
.setOptimizeReport(optimizeReport);
.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 @@ -219,15 +219,17 @@ public TestCommand() {
@CommandLine.Option(names = "--cloud", description = "Enable cloud artifact generation")
private String cloud;

@CommandLine.Option(names = "--optimize", description = "generate optimized executable jar", defaultValue = "false")
private Boolean optimizeCodegen;
@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 = "--optimize-report", description = "generate code generation optimization reports",
@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 optimizeReport;
private Boolean deadCodeEliminationReport;

private static final String testCmd = "bal test [--OPTIONS]\n" +
" [<ballerina-file> | <package-path>] [(-Ckey=value)...]";
Expand Down Expand Up @@ -436,8 +438,8 @@ private BuildOptions constructBuildOptions() {
.setGraalVMBuildOptions(graalVMBuildOptions)
.setShowDependencyDiagnostics(showDependencyDiagnostics)
.setOptimizeDependencyCompilation(optimizeDependencyCompilation)
.setOptimizeCodegen(optimizeCodegen)
.setOptimizeReport(optimizeReport);
.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 @@ -117,7 +117,7 @@ public void execute(Project project) {
addDiagnosticForProvidedPlatformLibs(project, diagnostics);
}
if (this.compileForBalBuild || this.compileForBalTest) {
addDiagnosticForInvalidOptimizeReportFlagUsage(project, diagnostics);
addDiagnosticForInvalidDeadCodeEliminationReportFlagUsage(project, diagnostics);
}

long start = 0;
Expand Down Expand Up @@ -342,12 +342,14 @@ private void addDiagnosticForProvidedPlatformLibs(Project project, List<Diagnost
}
}

private void addDiagnosticForInvalidOptimizeReportFlagUsage(Project project, List<Diagnostic> diagnostics) {
private void addDiagnosticForInvalidDeadCodeEliminationReportFlagUsage(Project project,
List<Diagnostic> diagnostics) {
BuildOptions buildOptions = project.buildOptions();
if (buildOptions.optimizeReport() && !buildOptions.optimizeCodegen()) {
if (buildOptions.deadCodeEliminationReport() && !buildOptions.eliminateDeadCode()) {
DiagnosticInfo diagnosticInfo = new DiagnosticInfo(
ProjectDiagnosticErrorCode.INVALID_VERBOSE_FLAG_USAGE.diagnosticId(),

Check warning on line 350 in cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java#L349-L350

Added lines #L349 - L350 were not covered by tests
"--optimize-report flag can only be used with --optimize flag", DiagnosticSeverity.ERROR);
"--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()));

Check warning on line 354 in cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java

View check run for this annotation

Codecov / codecov/patch

cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java#L353-L354

Added lines #L353 - L354 were not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void execute(Project project) {
out.println(warnings);
}
emitResult = jBallerinaBackend.emit(JBallerinaBackend.OutputType.GRAAL_EXEC, executablePath);
} else if (project.buildOptions().optimizeCodegen()) {
} else if (project.buildOptions().eliminateDeadCode()) {
emitResult = jBallerinaBackend.emit(JBallerinaBackend.OutputType.OPTIMIZE_CODEGEN, executablePath);
} else {
emitResult = jBallerinaBackend.emit(JBallerinaBackend.OutputType.EXEC, executablePath);
Expand Down Expand Up @@ -121,7 +121,7 @@ public void execute(Project project) {
throw createLauncherException(e.getMessage());
}

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

if (project.buildOptions().getTargetPath() != null) {
Expand All @@ -137,7 +137,7 @@ public void execute(Project project) {
}

// TODO: this has lot of common code with default case, refactor to common method
if (project.buildOptions().optimizeCodegen()) {
if (project.buildOptions().eliminateDeadCode()) {
Path relativePathToExecutable = currentDir.relativize(executablePath);
String relativePathToExecutableString =
relativePathToExecutable.toString().replace(BLANG_COMPILED_JAR_EXT, BYTECODE_OPTIMIZED_JAR_SUFFIX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ public boolean showDependencyDiagnostics() {
return toBooleanDefaultIfNull(this.showDependencyDiagnostics);
}

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

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

/**
Expand Down Expand Up @@ -219,8 +219,8 @@ public BuildOptions acceptTheirs(BuildOptions theirOptions) {
buildOptionsBuilder.setExportOpenAPI(compilationOptions.exportOpenAPI);
buildOptionsBuilder.setExportComponentModel(compilationOptions.exportComponentModel);
buildOptionsBuilder.setEnableCache(compilationOptions.enableCache);
buildOptionsBuilder.setOptimizeCodegen(compilationOptions.optimizeCodegen);
buildOptionsBuilder.setOptimizeReport(compilationOptions.optimizeReport);
buildOptionsBuilder.setEliminateDeadCode(compilationOptions.eliminateDeadCode);
buildOptionsBuilder.setDeadCodeEliminationReport(compilationOptions.deadCodeEliminationReport);
buildOptionsBuilder.setRemoteManagement(compilationOptions.remoteManagement);
buildOptionsBuilder.setOptimizeDependencyCompilation(compilationOptions.optimizeDependencyCompilation);

Expand Down Expand Up @@ -263,8 +263,8 @@ public enum OptionName {
GRAAL_VM_BUILD_OPTIONS("graalvmBuildOptions"),
SHOW_DEPENDENCY_DIAGNOSTICS("showDependencyDiagnostics"),
OPTIMIZE_DEPENDENCY_COMPILATION("optimizeDependencyCompilation"),
OPTIMIZE_CODEGEN("optimizeCodegen"),
OPTIMIZE_REPORT("optimizeReport");
ELIMINATE_DEAD_CODE("eliminateDeadCode"),
DEAD_CODE_ELIMINATION_REPORT("deadCodeEliminationReport");

private final String name;

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

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

Expand All @@ -445,8 +445,8 @@ public BuildOptionsBuilder setOptimizeDependencyCompilation(Boolean value) {
return this;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public class CompilationOptions {
Boolean disableSyntaxTree;
Boolean remoteManagement;
Boolean optimizeDependencyCompilation;
Boolean optimizeCodegen;
Boolean optimizeReport;
Boolean eliminateDeadCode;
Boolean deadCodeEliminationReport;
private CompilationOptions(Boolean offlineBuild, Boolean observabilityIncluded, Boolean dumpBir,
Boolean dumpBirFile, String cloud, Boolean listConflictedClasses, Boolean sticky,
Boolean dumpGraph, Boolean dumpRawGraphs, Boolean withCodeGenerators,
Boolean withCodeModifiers, Boolean configSchemaGen, Boolean exportOpenAPI,
Boolean exportComponentModel, Boolean enableCache, Boolean disableSyntaxTree,
Boolean remoteManagement, Boolean optimizeDependencyCompilation,
Boolean optimizeCodegen, Boolean optimizeReport) {
Boolean eliminateDeadCode, Boolean deadCodeEliminationReport) {
this.offlineBuild = offlineBuild;
this.observabilityIncluded = observabilityIncluded;
this.dumpBir = dumpBir;
Expand All @@ -65,8 +65,8 @@ private CompilationOptions(Boolean offlineBuild, Boolean observabilityIncluded,
this.exportOpenAPI = exportOpenAPI;
this.exportComponentModel = exportComponentModel;
this.enableCache = enableCache;
this.optimizeCodegen = optimizeCodegen;
this.optimizeReport = optimizeReport;
this.eliminateDeadCode = eliminateDeadCode;
this.deadCodeEliminationReport = deadCodeEliminationReport;
this.disableSyntaxTree = disableSyntaxTree;
this.remoteManagement = remoteManagement;
this.optimizeDependencyCompilation = optimizeDependencyCompilation;
Expand Down Expand Up @@ -132,12 +132,12 @@ public boolean enableCache() {
return toBooleanDefaultIfNull(this.enableCache);
}

public boolean optimizeCodegen() {
return toBooleanDefaultIfNull(optimizeCodegen);
public boolean eliminateDeadCode() {
return toBooleanDefaultIfNull(eliminateDeadCode);
}

public boolean optimizeReport() {
return toBooleanDefaultIfNull(optimizeReport);
public boolean deadCodeEliminationReport() {
return toBooleanDefaultIfNull(deadCodeEliminationReport);
}

boolean remoteManagement() {
Expand Down Expand Up @@ -241,15 +241,15 @@ CompilationOptions acceptTheirs(CompilationOptions theirOptions) {
} else {
compilationOptionsBuilder.setOptimizeDependencyCompilation(this.optimizeDependencyCompilation);
}
if (theirOptions.optimizeCodegen != null) {
compilationOptionsBuilder.setOptimizeCodegen(theirOptions.optimizeCodegen);
if (theirOptions.eliminateDeadCode != null) {
compilationOptionsBuilder.setEliminateDeadCode(theirOptions.eliminateDeadCode);
} else {
compilationOptionsBuilder.setOptimizeCodegen(this.optimizeCodegen);
compilationOptionsBuilder.setEliminateDeadCode(this.eliminateDeadCode);
}
if (theirOptions.optimizeReport != null) {
compilationOptionsBuilder.setOptimizeReport(theirOptions.optimizeReport);
if (theirOptions.deadCodeEliminationReport != null) {
compilationOptionsBuilder.setDeadCodeEliminationReport(theirOptions.deadCodeEliminationReport);
} else {
compilationOptionsBuilder.setOptimizeReport(this.optimizeReport);
compilationOptionsBuilder.setDeadCodeEliminationReport(this.deadCodeEliminationReport);
}
return compilationOptionsBuilder.build();
}
Expand Down Expand Up @@ -305,10 +305,10 @@ public static class CompilationOptionsBuilder {
private Boolean exportComponentModel;
private Boolean enableCache;
private Boolean disableSyntaxTree;
private static Boolean optimizeCodegen;
private static Boolean eliminatedDeadCode;
private Boolean remoteManagement;
private Boolean optimizeDependencyCompilation;
private static Boolean optimizeReport;
private static Boolean deadCodeEliminationReport;

public CompilationOptionsBuilder setOffline(Boolean value) {
offline = value;
Expand Down Expand Up @@ -385,16 +385,16 @@ CompilationOptionsBuilder setExportComponentModel(Boolean value) {
return this;
}

CompilationOptionsBuilder setOptimizeCodegen(Boolean value) {
if (optimizeCodegen == null) {
optimizeCodegen = value;
CompilationOptionsBuilder setEliminateDeadCode(Boolean value) {
if (eliminatedDeadCode == null) {
eliminatedDeadCode = value;
}
return this;
}

CompilationOptionsBuilder setOptimizeReport(Boolean value) {
if (optimizeReport == null) {
optimizeReport = value;
CompilationOptionsBuilder setDeadCodeEliminationReport(Boolean value) {
if (deadCodeEliminationReport == null) {
deadCodeEliminationReport = value;
}
return this;
}
Expand All @@ -419,7 +419,7 @@ public CompilationOptions build() {
dumpBirFile, cloud, listConflictedClasses, sticky, dumpGraph, dumpRawGraph,
withCodeGenerators, withCodeModifiers, configSchemaGen, exportOpenAPI,
exportComponentModel, enableCache, disableSyntaxTree, remoteManagement,
optimizeDependencyCompilation, optimizeCodegen, optimizeReport);
optimizeDependencyCompilation, eliminatedDeadCode, deadCodeEliminationReport);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.ballerina.projects.util.CodegenOptimizationConstants;
import org.ballerinalang.model.elements.PackageID;
import org.ballerinalang.model.symbols.SymbolOrigin;
import org.wso2.ballerinalang.compiler.bir.model.BIRNode;
Expand All @@ -41,33 +42,32 @@
import java.util.concurrent.ConcurrentHashMap;

/**
* Emits time durations and optimized node details for codegen optimization.
* Emits time durations and eliminated node details for dead code elimination.
*
* @since 2201.11.0
*/
public final class CodeGenOptimizationReportEmitter {
public final class DeadCodeEliminationReportEmitter {

private static final CompilerContext.Key<CodeGenOptimizationReportEmitter> CODEGEN_OPTIMIZATION_REPORT_EMITTER_KEY =
private static final CompilerContext.Key<DeadCodeEliminationReportEmitter> CODEGEN_OPTIMIZATION_REPORT_EMITTER_KEY =
new CompilerContext.Key<>();
private final Map<PackageID, Long> birOptimizationDurations;
private long nativeOptimizationDuration;
private final PrintStream out;
private static final String CODEGEN_OPTIMIZATION_REPORT = "codegen_optimization_report.json";

private CodeGenOptimizationReportEmitter(CompilerContext compilerContext) {
private DeadCodeEliminationReportEmitter(CompilerContext compilerContext) {
compilerContext.put(CODEGEN_OPTIMIZATION_REPORT_EMITTER_KEY, this);
this.out = System.out;
this.birOptimizationDurations = new ConcurrentHashMap<>();
this.nativeOptimizationDuration = 0;
}

public static CodeGenOptimizationReportEmitter getInstance(CompilerContext compilerContext) {
CodeGenOptimizationReportEmitter codegenOptimizationReportEmitter =
public static DeadCodeEliminationReportEmitter getInstance(CompilerContext compilerContext) {
DeadCodeEliminationReportEmitter codegenOptimizationReportEmitterDead =
compilerContext.get(CODEGEN_OPTIMIZATION_REPORT_EMITTER_KEY);
if (codegenOptimizationReportEmitter == null) {
codegenOptimizationReportEmitter = new CodeGenOptimizationReportEmitter(compilerContext);
if (codegenOptimizationReportEmitterDead == null) {
codegenOptimizationReportEmitterDead = new DeadCodeEliminationReportEmitter(compilerContext);
}
return codegenOptimizationReportEmitter;
return codegenOptimizationReportEmitterDead;
}

void flipBirOptimizationTimer(PackageID pkgId) {
Expand Down Expand Up @@ -123,7 +123,7 @@ void emitCodegenOptimizationReport(Map<PackageID, UsedBIRNodeAnalyzer.Invocation
// We are need to preserve the insertion order here since we use reports in tests.
Map<String, CodegenOptimizationReport> reports = new LinkedHashMap<>(invocationDataMap.size());
invocationDataMap.forEach((key, value) -> reports.put(key.toString(), getCodegenOptimizationReport(value)));
Path jsonFilePath = targetDirectoryPath.resolve(CODEGEN_OPTIMIZATION_REPORT);
Path jsonFilePath = targetDirectoryPath.resolve(CodegenOptimizationConstants.DEAD_CODE_ELIMINATION_REPORT);
File jsonFile = new File(jsonFilePath.toString());

try (Writer writer = new OutputStreamWriter(new FileOutputStream(jsonFile), StandardCharsets.UTF_8)) {
Expand Down
Loading

0 comments on commit f94714b

Please sign in to comment.