Skip to contentSkip to conversation

common/mlx5: use intrinsics instead of inline assembly

PATCH Superseded Andre Muezerie 3 participants 8 messages May 5, 2025 – Jun 10, 2025 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 common/mlx5: use intrinsics instead of inline assembly

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

---
 drivers/common/mlx5/mlx5_common.c | 13 +++++++++++--
 drivers/net/mlx5/mlx5_txpp.c      | 15 +++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_common.c +11 -2
index 4f25127582..e1ce17559d 100644
@@ -75,9 +75,18 @@ static inline void mlx5_cpu_id(unsigned int level,
 				unsigned int *eax, unsigned int *ebx,
 				unsigned int *ecx, unsigned int *edx)
 {
+#ifdef RTE_TOOLCHAIN_MSVC
+	int data[4];
+	__cpuid(data, level);
+	*eax = data[0];
+	*ebx = data[1];
+	*ecx = data[2];
+	*edx = data[3];
+#else
 	__asm__("cpuid\n\t"
-		: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
-		: "0" (level));
+	: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
+	: "0" (level));
+#endif
 }
 #endif
 
drivers/net/mlx5/mlx5_txpp.c +15 -0
index e6d3ad83e9..5bbaf668e6 100644
@@ -486,6 +486,20 @@ mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh)
 }
 
 #if defined(RTE_ARCH_X86_64)
+#ifdef RTE_TOOLCHAIN_MSVC
+static inline int
+mlx5_atomic128_compare_exchange(rte_int128_t *dst,
+				rte_int128_t *exp,
+				const rte_int128_t *src)
+{
+	return (int)_InterlockedCompareExchange128(
+		(int64_t volatile *)dst,
+		src->val[1], /* exchange high */
+		src->val[0], /* exchange low */
+		(int64_t *)exp /* comparand result */
+	);
+}
+#else
 static inline int
 mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 				rte_int128_t *exp,
@@ -510,6 +524,7 @@ mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 	return res;
 }
 #endif
+#endif
 
 static inline void
 mlx5_atomic_read_cqe(rte_int128_t *from, rte_int128_t *ts)
Signature · 1 line
2.49.0.vfs.0.2
#1 lore raw
Quoted 84 lines · Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

---
 drivers/common/mlx5/mlx5_common.c | 13 +++++++++++--
 drivers/net/mlx5/mlx5_txpp.c      | 15 +++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_common.c +11 -2
index 4f25127582..e1ce17559d 100644
@@ -75,9 +75,18 @@ static inline void mlx5_cpu_id(unsigned int level,
 				unsigned int *eax, unsigned int *ebx,
 				unsigned int *ecx, unsigned int *edx)
 {
+#ifdef RTE_TOOLCHAIN_MSVC
+	int data[4];
+	__cpuid(data, level);
+	*eax = data[0];
+	*ebx = data[1];
+	*ecx = data[2];
+	*edx = data[3];
+#else
 	__asm__("cpuid\n\t"
-		: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
-		: "0" (level));
+	: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
+	: "0" (level));
+#endif
 }
 #endif
 
drivers/net/mlx5/mlx5_txpp.c +15 -0
index e6d3ad83e9..5bbaf668e6 100644
@@ -486,6 +486,20 @@ mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh)
 }
 
 #if defined(RTE_ARCH_X86_64)
+#ifdef RTE_TOOLCHAIN_MSVC
+static inline int
+mlx5_atomic128_compare_exchange(rte_int128_t *dst,
+				rte_int128_t *exp,
+				const rte_int128_t *src)
+{
+	return (int)_InterlockedCompareExchange128(
+		(int64_t volatile *)dst,
+		src->val[1], /* exchange high */
+		src->val[0], /* exchange low */
+		(int64_t *)exp /* comparand result */
+	);
+}
+#else
 static inline int
 mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 				rte_int128_t *exp,
@@ -510,6 +524,7 @@ mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 	return res;
 }
 #endif
+#endif
 
 static inline void
 mlx5_atomic_read_cqe(rte_int128_t *from, rte_int128_t *ts)
Signature · 1 line
2.49.0.vfs.0.2

Would someone be able to review this patchset?
Thanks,

Andre Muezerie

#2 lore raw
In reply to Andre Muezerie

Hi,

Quoted 15 lines · Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

Quoted 4 lines

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

*snip*

Quoted 10 lines
drivers/net/mlx5/mlx5_txpp.c +2 -0
index e6d3ad83e9..5bbaf668e6 100644
@@ -486,6 +486,20 @@ mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh)
 }
 
 #if defined(RTE_ARCH_X86_64)
+#ifdef RTE_TOOLCHAIN_MSVC
+static inline int

drivers/net/mlx5/mlx5_txpp.c mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh) · line 500

Quoted 10 lines
drivers/net/mlx5/mlx5_txpp.c +10 -0
+mlx5_atomic128_compare_exchange(rte_int128_t *dst,
+				rte_int128_t *exp,
+				const rte_int128_t *src)
+{
+	return (int)_InterlockedCompareExchange128(
+		(int64_t volatile *)dst,
+		src->val[1], /* exchange high */
+		src->val[0], /* exchange low */
+		(int64_t *)exp /* comparand result */
+	);

There is one checkpatch warning to fix here:

	CHECK:OPEN_ENDED_LINE: Lines should not end with a '('
	#117: FILE: drivers/net/mlx5/mlx5_txpp.c:495:
	+	return (int)_InterlockedCompareExchange128(

Also, I don't think that comments for arguments are needed here.

Could you please organize the code as follows:

	return (int)_InterlockedCompareExchange128((int64_t volatile *)dst,
		src->val[1], src->val[0], (int64_t *)exp);

drivers/net/mlx5/mlx5_txpp.c mlx5_atomic128_compare_exchange(rte_int128_t *dst, · line 530

Quoted 13 lines
drivers/net/mlx5/mlx5_txpp.c +3 -0
+}
+#else
 static inline int
 mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 				rte_int128_t *exp,
@@ -510,6 +524,7 @@ mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 	return res;
 }
 #endif
+#endif
 
 static inline void
 mlx5_atomic_read_cqe(rte_int128_t *from, rte_int128_t *ts)

Best regards,
Dariusz Sosnowski

#3 lore raw
Quoted 52 lines · Dariusz Sosnowski

Hi,

Quoted 15 lines · Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

Quoted 4 lines

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

*snip*

Quoted 10 lines
drivers/net/mlx5/mlx5_txpp.c +2 -0
index e6d3ad83e9..5bbaf668e6 100644
@@ -486,6 +486,20 @@ mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh)
 }
 
 #if defined(RTE_ARCH_X86_64)
+#ifdef RTE_TOOLCHAIN_MSVC
+static inline int

drivers/net/mlx5/mlx5_txpp.c mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh) · line 500

Quoted 10 lines
drivers/net/mlx5/mlx5_txpp.c +10 -0
+mlx5_atomic128_compare_exchange(rte_int128_t *dst,
+				rte_int128_t *exp,
+				const rte_int128_t *src)
+{
+	return (int)_InterlockedCompareExchange128(
+		(int64_t volatile *)dst,
+		src->val[1], /* exchange high */
+		src->val[0], /* exchange low */
+		(int64_t *)exp /* comparand result */
+	);

There is one checkpatch warning to fix here:

	CHECK:OPEN_ENDED_LINE: Lines should not end with a '('
	#117: FILE: drivers/net/mlx5/mlx5_txpp.c:495:
	+	return (int)_InterlockedCompareExchange128(
Quoted 7 lines

Also, I don't think that comments for arguments are needed here.

Could you please organize the code as follows:

	return (int)_InterlockedCompareExchange128((int64_t volatile *)dst,
		src->val[1], src->val[0], (int64_t *)exp);

Sure. I sent out a v2 of this patch with the suggested change.

Quoted 16 lines

drivers/net/mlx5/mlx5_txpp.c mlx5_atomic128_compare_exchange(rte_int128_t *dst, · line 530

Quoted 13 lines
drivers/net/mlx5/mlx5_txpp.c +3 -0
+}
+#else
 static inline int
 mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 				rte_int128_t *exp,
@@ -510,6 +524,7 @@ mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 	return res;
 }
 #endif
+#endif
 
 static inline void
 mlx5_atomic_read_cqe(rte_int128_t *from, rte_int128_t *ts)

Best regards,
Dariusz Sosnowski

#4 lore raw

PATCH v2 1/1 common/mlx5: use intrinsics instead of inline assembly

In reply to Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

---
 drivers/common/mlx5/mlx5_common.c | 13 +++++++++++--
 drivers/net/mlx5/mlx5_txpp.c      | 11 +++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)
drivers/common/mlx5/mlx5_common.c +11 -2
index ebe327a950..fbe1578a84 100644
@@ -75,9 +75,18 @@ static inline void mlx5_cpu_id(unsigned int level,
 				unsigned int *eax, unsigned int *ebx,
 				unsigned int *ecx, unsigned int *edx)
 {
+#ifdef RTE_TOOLCHAIN_MSVC
+	int data[4];
+	__cpuid(data, level);
+	*eax = data[0];
+	*ebx = data[1];
+	*ecx = data[2];
+	*edx = data[3];
+#else
 	__asm__("cpuid\n\t"
-		: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
-		: "0" (level));
+	: "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
+	: "0" (level));
+#endif
 }
 #endif
 
drivers/net/mlx5/mlx5_txpp.c +11 -0
index e6d3ad83e9..0e99b58bde 100644
@@ -486,6 +486,16 @@ mlx5_txpp_cq_arm(struct mlx5_dev_ctx_shared *sh)
 }
 
 #if defined(RTE_ARCH_X86_64)
+#ifdef RTE_TOOLCHAIN_MSVC
+static inline int
+mlx5_atomic128_compare_exchange(rte_int128_t *dst,
+				rte_int128_t *exp,
+				const rte_int128_t *src)
+{
+	return (int)_InterlockedCompareExchange128((int64_t volatile *)dst,
+	       src->val[1], src->val[0], (int64_t *)exp);
+}
+#else
 static inline int
 mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 				rte_int128_t *exp,
@@ -510,6 +520,7 @@ mlx5_atomic128_compare_exchange(rte_int128_t *dst,
 	return res;
 }
 #endif
+#endif
 
 static inline void
 mlx5_atomic_read_cqe(rte_int128_t *from, rte_int128_t *ts)
Signature · 1 line
2.49.0.vfs.0.3
#5 lore raw

Hi,

Quoted 15 lines · Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

Quoted 4 lines

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

Thank you for applying the changes.

Best regards,
Dariusz Sosnowski

#6 lore raw
Quoted 21 lines · Dariusz Sosnowski

Hi,

Quoted 18 lines · Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

     missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

Quoted 6 lines
Quoted 1 line

Thank you for applying the changes.

Best regards,
Dariusz Sosnowski

Forgot about:

#7 lore raw
In reply to Andre Muezerie

Hi,

Quoted 15 lines · Andre Muezerie

When compiling with MSVC the errors below are hit because msvc does not support inline assembly:

1)
../drivers/common/mlx5/mlx5_common.c(86): warning C4013: '__asm__'
undefined; assuming extern returning int
../drivers/common/mlx5/mlx5_common.c(87): error C2143: syntax error:

      missing ')' before ':'

2)
../drivers/net/mlx5/mlx5_txpp.c(510): error C2065: '__asm__':
undeclared identifier
../drivers/net/mlx5/mlx5_txpp.c(510): error C2143: syntax error:
missing ';' before 'volatile'

Quoted 4 lines

The fix for (1) is to use compiler intrinsic __cpuid and for (2) intrinsic _InterlockedCompareExchange128 can be used.

patch applied to next-net-mlx,

Kindest regards
Raslan Darawsheh