Skip to content

[X86]Add support for __outbyte/word/dword and __inbyte/word/dword #93774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions clang/lib/Headers/intrin.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ static inline unsigned long _inpd(unsigned short port) {
return ret;
}

static inline int _outp(unsigned short port, int data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change it to __outbyte instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's 2 differences between _outp and __outbyte.

First, the newer intrinsics don't return a value.

Second, __outbyte signature is

void __outbyte(
   unsigned short Port,
   unsigned char Data
);

Note that the second input is unsigned char instead of int. This is likely because _outp is supposed to write a byte.

Other than that, I have verified that renaming to __outbyte works and is functionally equivalent.
When lowered via microsoft's cl, the asm is identical.

__asm__ volatile("outb %b0, %w1" : : "a"(data), "Nd"(port));
return data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return the direct data seems useless. Did you check if MSVC returns the same value or it actually reads from the port and returns the old data?

}

static inline unsigned short _outpw(unsigned short port, unsigned short data) {
__asm__ volatile("outw %w0, %w1" : : "a"(data), "Nd"(port));
return data;
}

static inline unsigned long _outpd(unsigned short port, unsigned long data) {
__asm__ volatile("outl %k0, %w1" : : "a"(data), "Nd"(port));
return data;
}
#endif

#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
Expand Down
27 changes: 27 additions & 0 deletions clang/test/CodeGen/X86/ms-x86-intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ unsigned long test_inpd(unsigned short port) {
// CHECK: [[TMP0:%.*]] = tail call i32 asm sideeffect "inl ${1:w}, ${0:k}", "={ax},N{dx},~{dirflag},~{fpsr},~{flags}"(i16 [[PORT]])
// CHECK-NEXT: ret i32 [[TMP0]]

int test_outp(unsigned short port, int data) {
return _outp(port, data);
}
// CHECK-LABEL: i32 @test_outp(
// CHECK-SAME: [[PORT:%.*]],
// CHECK-SAME: [[DATA:%.*]])
// CHECK: tail call void asm sideeffect "outb ${0:b}, ${1:w}", "{ax},N{dx},~{dirflag},~{fpsr},~{flags}"(i32 [[DATA]], i16 [[PORT]])
// CHECK-NEXT: ret i32 [[DATA]]

unsigned short test_outpw(unsigned short port, unsigned short data) {
return _outpw(port, data);
}
// CHECK-LABEL: i16 @test_outpw(
// CHECK-SAME: [[PORT:%.*]],
// CHECK-SAME: [[DATA:%.*]])
// CHECK: tail call void asm sideeffect "outw ${0:w}, ${1:w}", "{ax},N{dx},~{dirflag},~{fpsr},~{flags}"(i16 [[DATA]], i16 [[PORT]])
// CHECK-NEXT: ret i16 [[DATA]]

unsigned long test_outpd(unsigned short port, unsigned long data) {
return _outpd(port, data);
}
// CHECK-LABEL: i32 @test_outpd(
// CHECK-SAME: [[PORT:%.*]],
// CHECK-SAME: [[DATA:%.*]])
// CHECK: tail call void asm sideeffect "outl ${0:k}, ${1:w}", "{ax},N{dx},~{dirflag},~{fpsr},~{flags}"(i32 [[DATA]], i16 [[PORT]])
// CHECK-NEXT: ret i32 [[DATA]]

#if defined(__x86_64__)

char test__readgsbyte(unsigned long Offset) {
Expand Down
Loading