Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gustavo Sousa <gustavo.sousa@intel.com>
To: intel-xe@lists.freedesktop.org
Cc: Gustavo Sousa <gustavo.sousa@intel.com>
Subject: [PATCH 6/6] drm/xe/rtp: Implement a structured parser for rule matching
Date: Wed, 14 Jan 2026 19:49:56 -0300	[thread overview]
Message-ID: <20260114-rtp-rule-parser-v1-6-fa9029586bff@intel.com> (raw)
In-Reply-To: <20260114-rtp-rule-parser-v1-0-fa9029586bff@intel.com>

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 ?;

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>
---
 drivers/gpu/drm/xe/xe_rtp.c | 134 ++++++++++++++++++++++++++++----------------
 1 file changed, 85 insertions(+), 49 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_rtp.c b/drivers/gpu/drm/xe/xe_rtp.c
index 55df9c16a3cc..1e3d89e1f2a5 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;
@@ -114,65 +131,84 @@ static bool rule_match_item(const struct xe_device *xe,
 	}
 }
 
-static bool rule_matches_with_err(const struct xe_device *xe,
-				  struct xe_gt *gt,
-				  struct xe_hw_engine *hwe,
-				  const struct xe_rtp_rule *rules,
-				  unsigned int n_rules,
-				  int *err)
+/*
+ * 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)
 {
-	const struct xe_rtp_rule *r;
-	unsigned int i, rcount = 0;
-	bool parse_only = false;
-	bool match = false;
+	bool match = true;
+	unsigned int count = 0;
 
-	if (err)
-		*err = 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);
 
-	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)) {
-				parse_only = true;
-				match = false;
-				if (err)
-					*err = -EINVAL;
-			} else if (match) {
-				parse_only = true;
-			}
+		if (!match)
+			parse_only = true;
 
-			rcount = 0;
+		match_ctx->head++;
+		count++;
+	}
 
-			continue;
-		}
+	if (drm_WARN_ON(&match_ctx->xe->drm, !count))
+		match_ctx->err = -EINVAL;
 
-		rcount++;
+	return match;
+}
 
-		if (parse_only || !rule_match_item(xe, gt, hwe, r)) {
-			if (!parse_only)
-				match = false;
+/*
+ * 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);
 
-			/*
-			 * Advance rules until we find XE_RTP_MATCH_OR to check
-			 * if there's another set of conditions to check
-			 */
-			while (i + 1 < n_rules && rules[i + 1].match_type != XE_RTP_MATCH_OR) {
-				i++;
-				rcount++;
-			}
-		} else {
-			match = true;
-		}
-	}
+	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++;
 
-	if (drm_WARN_ON(&xe->drm, !rcount)) {
-		match = false;
-		if (err)
-			*err = -EINVAL;
+		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,
+				  const struct xe_rtp_rule *rules,
+				  unsigned int n_rules,
+				  int *err)
+{
+	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 = match_ctx.err;
+
+	if (drm_WARN_ON(&xe->drm, match_ctx.err))
+		return false;
+
+	return match;
+}
+
 static bool rule_matches(const struct xe_device *xe,
 			 struct xe_gt *gt,
 			 struct xe_hw_engine *hwe,

-- 
2.52.0


  parent reply	other threads:[~2026-01-14 22:50 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-14 22:49 [PATCH 0/6] drm/xe/rtp: Miscellaneous improvements to rule matching Gustavo Sousa
2026-01-14 22:49 ` [PATCH 1/6] drm/xe/rtp: Write kunit test cases specific for " Gustavo Sousa
2026-01-14 22:57   ` Gustavo Sousa
2026-02-10 22:03   ` Matt Roper
2026-01-14 22:49 ` [PATCH 2/6] drm/xe/rtp: Drop rule matching cases from rtp_to_sr_cases and rtp_cases Gustavo Sousa
2026-02-10 22:06   ` Matt Roper
2026-01-14 22:49 ` [PATCH 3/6] drm/xe/rtp: Do not break parsing when missing context Gustavo Sousa
2026-02-10 22:20   ` Matt Roper
2026-04-29 19:45     ` Gustavo Sousa
2026-01-14 22:49 ` [PATCH 4/6] drm/xe/rtp: Extract rule_match_item() Gustavo Sousa
2026-02-10 22:24   ` Matt Roper
2026-01-14 22:49 ` [PATCH 5/6] drm/xe/rtp: Fully parse the ruleset Gustavo Sousa
2026-02-10 22:34   ` Matt Roper
2026-04-30 13:33     ` Gustavo Sousa
2026-01-14 22:49 ` Gustavo Sousa [this message]
2026-01-14 22:56 ` ✗ CI.checkpatch: warning for drm/xe/rtp: Miscellaneous improvements to rule matching Patchwork
2026-01-14 22:57 ` ✓ CI.KUnit: success " Patchwork
2026-01-14 23:30 ` ✓ Xe.CI.BAT: " Patchwork
2026-01-15  4:53 ` ✗ 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=20260114-rtp-rule-parser-v1-6-fa9029586bff@intel.com \
    --to=gustavo.sousa@intel.com \
    --cc=intel-xe@lists.freedesktop.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox