All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gustavo Sousa <gustavo.sousa@intel.com>
To: Matt Roper <matthew.d.roper@intel.com>
Cc: <intel-xe@lists.freedesktop.org>
Subject: Re: [PATCH v2 7/7] drm/xe/rtp: Implement a structured parser for rule matching
Date: Thu, 14 May 2026 17:19:03 -0300	[thread overview]
Message-ID: <87y0hlvj60.fsf@intel.com> (raw)
In-Reply-To: <20260513233435.GH2131374@mdroper-desk1.amr.corp.intel.com>

Matt Roper <matthew.d.roper@intel.com> writes:

> On Thu, Apr 30, 2026 at 05:20:08PM -0300, Gustavo Sousa wrote:
>> The current unwritten grammar for RTP rules is as follows:
>> 
>>           rules = disjunction;
>>     disjunction = conjunction { "OR" conjunction };
>>     conjunction = single_rule { single_rule }
>>                               /* AND operator is implicit */;
>>     single_rule = ? GRAPHICS_VERSION(...), MEDIA_VERSION(...),
>>                      FUNC(...), etc ?;
>
> We should probably add this grammar BNF to the DOC: section at the top
> of the file too.  Then it won't be "unwritten" anymore.  :-)

Cool. It could be on the kernel-doc for XE_RTP_RULES(). Ack on
incrementing this patch with the following then?

    diff --git a/drivers/gpu/drm/xe/xe_rtp.h b/drivers/gpu/drm/xe/xe_rtp.h
    index 562082b18d7b..2a8e536eaa5d 100644
    --- a/drivers/gpu/drm/xe/xe_rtp.h
    +++ b/drivers/gpu/drm/xe/xe_rtp.h
    @@ -394,18 +394,39 @@ struct xe_reg_sr;
      * XE_RTP_RULES - Helper to set multiple rules to a struct xe_rtp_entry_sr entry
      * @...: Rules
      *
    - * At least one rule is needed and up to 12 are supported. Multiple rules are
    - * AND'ed together, i.e. all the rules must evaluate to true for the entry to
    - * be processed. See XE_RTP_MATCH_* for the possible match rules. Example:
    + * When an RTP table is being processed, the rules of each entry are evaluated
    + * to check if they match the target entity (platform, gt or hwe, depending on
    + * the specific RTP table).
    + *
    + * The sequence of arguments of this macro must follow the following eBNF
    + * grammar::
    + *
    + *            rules = disjunction;
    + *      disjunction = conjunction, { "OR" conjunction };
    + *      conjunction = single_rule, { single_rule };
    + *				   (* the AND operator is implicit *)
    + *      single_rule = ? GRAPHICS_VERSION(...), MEDIA_VERSION(...),
    + *                      FUNC(...), etc ?
    + *
    + * Examples:
      *
      * .. code-block:: c
      *
      *	const struct xe_rtp_entry_sr wa_entries[] = {
      *		...
    - *		{ XE_RTP_NAME("test-entry"),
    + *		{ XE_RTP_NAME("entry-a"),
    + *		  // Match DG2-G10 with graphics steppings A0 up-to B0
    + *		  // (exclusive).
      *		  XE_RTP_RULES(SUBPLATFORM(DG2, G10), GRAPHICS_STEP(A0, B0)),
      *		  ...
      *		},
    + *		{ XE_RTP_NAME("entry-b"),
    + *		  // Match graphics version 20 (all steppings) or graphics
    + *		  // version 30 steppings A0 up-to B0 (exclusive).
    + *		  XE_RTP_RULES(GRAPHICS_VERSION(2000), OR,
    + *			       GRAPHICS_VERSION(3000), GRAPHICS_STEP(A0, B0))
    + *		  ...
    + *		},
      *		...
      *	};
      */

--
Gustavo Sousa

>
>> 
>> While rule_matches() currently works for the grammar above, it doesn't
>> easily resemble it.  Let's replace it with an implementation that is
>> structured in a way to resemble the grammar.
>> 
>> Such a new implementation, although a bit more verbose, is arguably
>> easier to reason about and to adapt to any extension we do to the
>> grammer in the future.
>> 
>> Signed-off-by: Gustavo Sousa <gustavo.sousa@intel.com>
>
> Aside adding the kerneldoc, this looks good to me.  It will definitely
> make it easier to define and implement additions to the grammar in the
> future, if necessary.
>
>
> Reviewed-by: Matt Roper <matthew.d.roper@intel.com>
>
>> ---
>>  drivers/gpu/drm/xe/xe_rtp.c | 138 ++++++++++++++++++++++++++++----------------
>>  1 file changed, 88 insertions(+), 50 deletions(-)
>> 
>> diff --git a/drivers/gpu/drm/xe/xe_rtp.c b/drivers/gpu/drm/xe/xe_rtp.c
>> index 976a2e1f5592..dec9d94e6fb0 100644
>> --- a/drivers/gpu/drm/xe/xe_rtp.c
>> +++ b/drivers/gpu/drm/xe/xe_rtp.c
>> @@ -30,11 +30,28 @@ static bool has_samedia(const struct xe_device *xe)
>>  	return xe->info.media_verx100 >= 1300;
>>  }
>>  
>> -static bool rule_match_item(const struct xe_device *xe,
>> -			    struct xe_gt *gt,
>> -			    struct xe_hw_engine *hwe,
>> -			    const struct xe_rtp_rule *r)
>> +struct rule_match_ctx {
>> +	const struct xe_device *xe;
>> +	struct xe_gt *gt;
>> +	struct xe_hw_engine *hwe;
>> +	const struct xe_rtp_rule *rules;
>> +	const unsigned int n_rules;
>> +	unsigned int head;
>> +	int err;
>> +};
>> +
>> +static bool rule_is_item(const struct xe_rtp_rule *r)
>> +{
>> +	return r->match_type != XE_RTP_MATCH_OR;
>> +}
>> +
>> +static bool rule_match_item(struct rule_match_ctx *match_ctx)
>>  {
>> +	const struct xe_device *xe = match_ctx->xe;
>> +	struct xe_gt *gt = match_ctx->gt;
>> +	struct xe_hw_engine *hwe = match_ctx->hwe;
>> +	const struct xe_rtp_rule *r = &match_ctx->rules[match_ctx->head];
>> +
>>  	switch (r->match_type) {
>>  	case XE_RTP_MATCH_PLATFORM:
>>  		return xe->info.platform == r->platform;
>> @@ -120,6 +137,63 @@ static bool rule_match_item(const struct xe_device *xe,
>>  	}
>>  }
>>  
>> +/*
>> + * Match a conjunctive set of rules (rules joined by an implicit "AND").
>> + *
>> + * Once one item evaluates to false, the remaining items are not evaluated
>> + * anymore.  Nevetheless, all rules are consumed to allow detecting syntax
>> + * errors.
>> + */
>> +static bool rule_match_and(struct rule_match_ctx *match_ctx, bool parse_only)
>> +{
>> +	bool match = true;
>> +	unsigned int count = 0;
>> +
>> +	while (match_ctx->head < match_ctx->n_rules &&
>> +	       rule_is_item(&match_ctx->rules[match_ctx->head])) {
>> +		if (!parse_only)
>> +			match = rule_match_item(match_ctx);
>> +
>> +		if (!match)
>> +			parse_only = true;
>> +
>> +		match_ctx->head++;
>> +		count++;
>> +	}
>> +
>> +	if (drm_WARN_ON(&match_ctx->xe->drm, !count)) {
>> +		match_ctx->err = -EINVAL;
>> +
>> +		if (!parse_only)
>> +			match = false;
>> +	}
>> +
>> +	return match;
>> +}
>> +
>> +/*
>> + * Match a disjunctive set of rules (subset of rules joined by
>> + * "XE_RTP_MATCH_OR").
>> + *
>> + * Once one subset evaluates to true, the remaining items are not evaluated
>> + * anymore. Nevetheless, all rules are consumed to allow detecting syntax
>> + * errors.
>> + */
>> +static bool rule_match_or(struct rule_match_ctx *match_ctx)
>> +{
>> +	bool match = rule_match_and(match_ctx, false);
>> +
>> +	while (match_ctx->head < match_ctx->n_rules &&
>> +	       match_ctx->rules[match_ctx->head].match_type == XE_RTP_MATCH_OR) {
>> +		/* Consume XE_RTP_MATCH_OR. */
>> +		match_ctx->head++;
>> +
>> +		match = rule_match_and(match_ctx, match);
>> +	}
>> +
>> +	return match;
>> +}
>> +
>>  static bool rule_matches_with_err(const struct xe_device *xe,
>>  				  struct xe_gt *gt,
>>  				  struct xe_hw_engine *hwe,
>> @@ -127,55 +201,19 @@ static bool rule_matches_with_err(const struct xe_device *xe,
>>  				  unsigned int n_rules,
>>  				  int *err)
>>  {
>> -	const struct xe_rtp_rule *r;
>> -	unsigned int i, rcount = 0;
>> -	bool short_circuit_or = false;
>> +	struct rule_match_ctx match_ctx = {
>> +		.xe = xe,
>> +		.gt = gt,
>> +		.hwe = hwe,
>> +		.rules = rules,
>> +		.n_rules = n_rules,
>> +	};
>> +	bool match = rule_match_or(&match_ctx);
>>  
>>  	if (err)
>> -		*err = 0;
>> -
>> -	for (r = rules, i = 0; i < n_rules; r = &rules[++i]) {
>> -		if (r->match_type == XE_RTP_MATCH_OR) {
>> -			if (drm_WARN_ON(&xe->drm, !rcount)) {
>> -				if (err)
>> -					*err = -EINVAL;
>> -				continue;
>> -			}
>> -
>> -			/*
>> -			 * This is only reached if a complete conjunction of
>> -			 * rules passed, in which case we short-circuit rule
>> -			 * evaluation, but still keep parsing to find any syntax
>> -			 * errors.
>> -			 */
>> -			short_circuit_or = true;
>> -			rcount = 0;
>> -			continue;
>> -		}
>> -
>> -		if (short_circuit_or || rule_match_item(xe, gt, hwe, r)) {
>> -			rcount++;
>> -		} else {
>> -			/*
>> -			 * Advance rules until we find XE_RTP_MATCH_OR to check
>> -			 * if there's another set of conditions to check
>> -			 */
>> -			while (++i < n_rules && rules[i].match_type != XE_RTP_MATCH_OR)
>> -				;
>> -
>> -			if (i >= n_rules)
>> -				return false;
>> -
>> -			rcount = 0;
>> -		}
>> -	}
>> -
>> -	if (drm_WARN_ON(&xe->drm, !rcount)) {
>> -		if (err)
>> -			*err = -EINVAL;
>> -	}
>> +		*err = match_ctx.err;
>>  
>> -	return short_circuit_or || rcount;
>> +	return match;
>>  }
>>  
>>  static bool rule_matches(const struct xe_device *xe,
>> 
>> -- 
>> 2.53.0
>> 
>
> -- 
> Matt Roper
> Graphics Software Engineer
> Linux GPU Platform Enablement
> Intel Corporation

  reply	other threads:[~2026-05-14 20:19 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30 20:20 [PATCH v2 0/7] drm/xe/rtp: Miscellaneous improvements to rule matching Gustavo Sousa
2026-04-30 20:20 ` [PATCH v2 1/7] drm/xe/rtp: Write kunit test cases specific for " Gustavo Sousa
2026-04-30 20:20 ` [PATCH v2 2/7] drm/xe/rtp: Drop rule matching cases from rtp_to_sr_cases and rtp_cases Gustavo Sousa
2026-04-30 20:20 ` [PATCH v2 3/7] drm/xe/rtp: Don't short-circuit to false in or-yes case Gustavo Sousa
2026-05-13 21:52   ` Violet Monti
2026-05-13 23:04   ` Matt Roper
2026-04-30 20:20 ` [PATCH v2 4/7] drm/xe/rtp: Do not break parsing when missing context Gustavo Sousa
2026-04-30 20:20 ` [PATCH v2 5/7] drm/xe/rtp: Extract rule_match_item() Gustavo Sousa
2026-04-30 20:20 ` [PATCH v2 6/7] drm/xe/rtp: Fully parse the ruleset Gustavo Sousa
2026-05-13 22:07   ` Violet Monti
2026-05-13 23:14   ` Matt Roper
2026-04-30 20:20 ` [PATCH v2 7/7] drm/xe/rtp: Implement a structured parser for rule matching Gustavo Sousa
2026-05-13 22:09   ` Violet Monti
2026-05-13 23:34   ` Matt Roper
2026-05-14 20:19     ` Gustavo Sousa [this message]
2026-05-14 20:51       ` Matt Roper
2026-04-30 21:02 ` ✗ CI.checkpatch: warning for drm/xe/rtp: Miscellaneous improvements to rule matching (rev2) Patchwork
2026-04-30 21:03 ` ✓ CI.KUnit: success " Patchwork
2026-04-30 21:59 ` ✓ Xe.CI.BAT: " Patchwork
2026-05-01  9:25 ` ✗ Xe.CI.FULL: failure " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y0hlvj60.fsf@intel.com \
    --to=gustavo.sousa@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=matthew.d.roper@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.