Skip to content

Commit

Permalink
Simplified parsing of PTX expressions using regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
MoFtZ authored and m4rs-mt committed May 20, 2021
1 parent 44695f3 commit 28d2613
Showing 1 changed file with 25 additions and 43 deletions.
68 changes: 25 additions & 43 deletions Src/ILGPU/Frontend/Intrinsic/LanguageIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ILGPU.Util;
using System;
using System.Collections.Immutable;
using System.Text.RegularExpressions;
using static ILGPU.Util.FormatString;
using FormatArray = System.Collections.Immutable.ImmutableArray<
ILGPU.Util.FormatString.FormatExpression>;
Expand Down Expand Up @@ -47,6 +48,13 @@ public LanguageIntrinsicAttribute(LanguageIntrinsicKind intrinsicKind)

partial class Intrinsics
{
/// <summary>
/// Regex for parsing PTX assembly instructions.
/// </summary>
private static readonly Regex PTXExpressionRegex =
// Escape sequence, %n arguments, singular % detection.
new Regex("(%%|%\\d+|%)");

/// <summary>
/// Handles language operations.
/// </summary>
Expand Down Expand Up @@ -165,59 +173,33 @@ public static bool TryParse(
string ptxExpression,
out FormatArray expressions)
{
expressions = FormatArray.Empty;

// Search for '%n' format arguments
var result = ImmutableArray.CreateBuilder<FormatExpression>(10);
var expression = ptxExpression.AsSpan();
var parts = PTXExpressionRegex.Split(ptxExpression);
var result = ImmutableArray.CreateBuilder<FormatExpression>(parts.Length);

while (expression.Length > 0)
foreach (var part in parts)
{
// Search for next %
int foundIndex = expression.IndexOf('%');
if (foundIndex < 0)
if (part == "%%")
{
result.Add(new FormatExpression(expression));
break;
result.Add(new FormatExpression("%"));
}

var escaped =
foundIndex + 1 < expression.Length &&
expression[foundIndex] == expression[foundIndex + 1];
if (escaped)
{
result.Add(new FormatExpression(expression.Slice(0, foundIndex + 1)));
expression = expression.Slice(foundIndex + 2);
}
else
else if (part.StartsWith("%"))
{
// Append text before new argument
if (foundIndex > 0)
// Check whether the argument can be resolved to an integer.
if (int.TryParse(part.Substring(1), out int argument))
{
result.Add(new FormatExpression(
expression.Slice(0, foundIndex)));
result.Add(new FormatExpression(argument));
}
expression = expression.Slice(foundIndex + 1);

// Retrieve all the numeric text
if (expression.Length == 0)
return false;

int endIndex = 0;
while (endIndex < expression.Length &&
char.IsDigit(expression[endIndex]))
else
{
endIndex++;
}

// Check whether the argument can be resolved to an integer
var numericExpr = expression.Slice(0, endIndex);
if (!int.TryParse(numericExpr.ToString(), out int argument))
// Singular % or remaining text was not a number.
expressions = FormatArray.Empty;
return false;

// Append current argument
result.Add(new FormatExpression(argument));
expression = expression.Slice(endIndex);
}
}
else if (part.Length > 0)
{
result.Add(new FormatExpression(part));
}
}

Expand Down

0 comments on commit 28d2613

Please sign in to comment.