Skip to content

Commit

Permalink
Modified OpenCL generator to forward declare all variables. (#1088)
Browse files Browse the repository at this point in the history
* Modified OpenCL generator to forward declare all variables.
- Backported #1084.

Co-authored-by: Ivan Pavlovic <[email protected]>
Co-authored-by: Jonathan Giannuzzi <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent 30aabb5 commit df6b23a
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 17 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/check-required.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Check required jobs

# This workflow is triggered when a workflow run for the CI is completed.
# It checks if the "All required checks done" job was actually successful
# (and not just skipped) and creates a check run if that is the case. The
# check run can be used to protect the main branch from being merged if the
# CI is not passing.

on:
workflow_run:
types: [completed]
workflows:
- CI
- Deploy Site with Jekyll, GitHub Pages

permissions:
actions: read
checks: write

jobs:
required-jobs:
name: Check required jobs
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
// list jobs for worklow run attempt
const { data: { jobs } } = await github.rest.actions.listJobsForWorkflowRunAttempt({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
run_id: context.payload.workflow_run.id,
attempt_number: context.payload.workflow_run.run_attempt,
});
// check if required job was successful
var success = false;
core.info(`Checking jobs for workflow run ${context.payload.workflow_run.html_url}`);
jobs.forEach(job => {
var mark = '-'
if (job.name === 'All required checks done' && job.conclusion === 'success') {
success = true;
mark = '✅';
}
core.info(`${mark} ${job.name}: ${job.conclusion}`);
});
// create check run if job was successful
if (success) {
await github.rest.checks.create({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
name: 'All required checks succeeded',
head_sha: context.payload.workflow_run.head_sha,
status: 'completed',
conclusion: 'success',
output: {
title: 'All required checks succeeded',
summary: `See [workflow run](${context.payload.workflow_run.html_url}) for details.`,
},
});
}
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ jobs:

# Virtual job that can be configured as a required check before a PR can be
# merged.
# As GitHub considers a check as successful if it is skipped, we need to
# check its status in another workflow (check-required.yml) and create a
# check there.
all-required-checks-done:
needs:
- check-style
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/deploy-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ jobs:
uses: actions/upload-pages-artifact@v2
##### GitHub Pages #####

# Virtual job that can be configured as a required check before a PR can be
# merged.
# As GitHub considers a check as successful if it is skipped, we need to
# check its status in another workflow (check-required.yml) and create a
# check there.
all-required-checks-done:
needs:
- build
Expand Down
63 changes: 56 additions & 7 deletions Src/ILGPU/Backends/OpenCL/CLCodeGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public struct StatementEmitter : IDisposable
#region Instance

private readonly StringBuilder stringBuilder;
private readonly StringBuilder variableBuilder;
private bool argMode;
private int argumentCount;

Expand All @@ -58,6 +59,7 @@ internal StatementEmitter(CLCodeGenerator codeGenerator)
{
CodeGenerator = codeGenerator;
stringBuilder = codeGenerator.Builder;
variableBuilder = codeGenerator.VariableBuilder;
argumentCount = 0;
argMode = false;

Expand Down Expand Up @@ -85,20 +87,23 @@ internal StatementEmitter(CLCodeGenerator codeGenerator)
private void BeginAppendTarget(Variable target, bool appendNew = true)
{
if (appendNew)
{
var variableType = CodeGenerator.GetVariableType(target);
stringBuilder.Append(variableType);
stringBuilder.Append(' ');
}
AppendDeclaration(target);
stringBuilder.Append(target.ToString());
}

/// <summary>
/// Appends a target declaration.
/// </summary>
/// <param name="target">The target declaration.</param>
internal void AppendDeclaration(Variable target) =>
BeginAppendTarget(target);
internal void AppendDeclaration(Variable target)
{
var variableType = CodeGenerator.GetVariableType(target);
variableBuilder.Append('\t');
variableBuilder.Append(variableType);
variableBuilder.Append(' ');
variableBuilder.Append(target.ToString());
variableBuilder.AppendLine(";");
}

/// <summary>
/// Appends a target.
Expand Down Expand Up @@ -828,6 +833,50 @@ public StatementEmitter BeginStatement(FormattableString command)
return emitter;
}

/// <summary>
/// Begins the function body, switching to variable capturing mode.
/// </summary>
protected void BeginFunctionBody()
{
// Start the function body.
Builder.AppendLine("{");
PushIndent();

#if DEBUG
Builder.AppendLine();
Builder.AppendLine("\t// Variable declarations");
Builder.AppendLine();
#endif

// Switch to the alternate builder, so that we can capture the code and
// variable declarations separately.
prefixBuilder = Builder;
Builder = suffixBuilder;
}

/// <summary>
/// Finishes the function body, ending variable capturing mode.
/// </summary>
protected void FinishFunctionBody()
{
// Restore the original builder, containing code before the variable
// declarations.
Builder = prefixBuilder;

// Add the variable declarations at the start of the function, to avoid
// issues with OpenCL compilers that are not C99 compliant, and cannot
// handle variable declarations intermingled with other code.
Builder.Append(VariableBuilder);
Builder.AppendLine();

// Add the code that was generated along with the variable declarations.
Builder.Append(suffixBuilder);

// Close the function body.
PopIndent();
Builder.AppendLine("}");
}

#endregion
}
}
12 changes: 10 additions & 2 deletions Src/ILGPU/Backends/OpenCL/CLCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ protected static string GetParameterName(Parameter parameter) =>
new Dictionary<BasicBlock, string>();
private readonly string labelPrefix;

private StringBuilder prefixBuilder = new StringBuilder();
private StringBuilder suffixBuilder = new StringBuilder();

/// <summary>
/// Constructs a new code generator.
/// </summary>
Expand All @@ -295,7 +298,7 @@ internal CLCodeGenerator(in GeneratorArgs args, Method method, Allocas allocas)

labelPrefix = "L_" + Method.Id.ToString();

Builder = new StringBuilder();
Builder = prefixBuilder;
}

#endregion
Expand Down Expand Up @@ -327,7 +330,12 @@ public IntrinsicImplementationProvider<CLIntrinsic.Handler>
/// <summary>
/// Returns the associated string builder.
/// </summary>
public StringBuilder Builder { get; }
public StringBuilder Builder { get; private set; }

/// <summary>
/// Returns the associated string builder.
/// </summary>
public StringBuilder VariableBuilder { get; } = new StringBuilder();

#endregion

Expand Down
6 changes: 2 additions & 4 deletions Src/ILGPU/Backends/OpenCL/CLFunctionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ public override void GenerateCode()
BindSharedMemoryAllocation(Allocas.DynamicSharedAllocations);

// Generate code
Builder.AppendLine("{");
PushIndent();
BeginFunctionBody();
GenerateCodeInternal();
PopIndent();
Builder.AppendLine("}");
FinishFunctionBody();
}

#endregion
Expand Down
6 changes: 2 additions & 4 deletions Src/ILGPU/Backends/OpenCL/CLKernelFunctionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public override void GenerateCode()
Builder.AppendLine(")");

// Emit code that moves view arguments into their appropriate targets
Builder.AppendLine("{");
PushIndent();
BeginFunctionBody();
GenerateArgumentMapping();

// Emit index computation
Expand Down Expand Up @@ -323,8 +322,7 @@ public override void GenerateCode()

// Generate code
GenerateCodeInternal();
PopIndent();
Builder.AppendLine("}");
FinishFunctionBody();
}

/// <summary>
Expand Down

0 comments on commit df6b23a

Please sign in to comment.