Skip to content

[CIR][CIRGen][builtin][X86] Implement support for _mm_getcsr() and _mm_setcsr() intrinsic #1681

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,40 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
getLoc(E->getExprLoc()), builder.getStringAttr("x86.rdtsc"), intTy)
.getResult();
}
case X86::BI__builtin_ia32_stmxcsr:
case X86::BI_mm_getcsr: {
// note that _mm_getcsr() returns uint, but llvm.x86.sse.stmxcsr takes i32
// pointer and returns void. So needs alloc extra memory to store the
// result.
auto loc = getLoc(E->getExprLoc());
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to match OG here, any reason not to use createMemTemp?

mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
mlir::Type i32Ty = cir::IntType::get(&getMLIRContext(), 32, true);
auto i32PtrTy = builder.getPointerTo(i32Ty);
// Allocate memory for the result
auto alloca = builder.createAlloca(loc, i32PtrTy, i32Ty, "csrRes",
builder.getAlignmentAttr(4));
builder.create<cir::LLVMIntrinsicCallOp>(
loc, builder.getStringAttr("x86.sse.stmxcsr"), voidTy, alloca);
// Load the value from the allocated memory
auto loadResult =
builder.createAlignedLoad(loc, i32Ty, alloca, llvm::Align(4));
return loadResult;
}
case X86::BI__builtin_ia32_ldmxcsr:
case X86::BI_mm_setcsr: {
auto loc = getLoc(E->getExprLoc());
Copy link
Member

Choose a reason for hiding this comment

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

Similar question for this one!

mlir::Type voidTy = cir::VoidType::get(&getMLIRContext());
mlir::Type i32Ty = cir::IntType::get(&getMLIRContext(), 32, true);
auto i32PtrTy = builder.getPointerTo(i32Ty);
// Allocate memory for the argument
auto alloca = builder.createAlloca(loc, i32PtrTy, i32Ty, "csrVal",
builder.getAlignmentAttr(4));
// Store the value to be set
builder.createAlignedStore(loc, Ops[0], alloca, CharUnits::fromQuantity(4));
return builder
.create<cir::LLVMIntrinsicCallOp>(
loc, builder.getStringAttr("x86.sse.ldmxcsr"), voidTy, alloca)
.getResult();
}
}
}
36 changes: 36 additions & 0 deletions clang/test/CIR/CodeGen/X86/builtins-x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,39 @@ unsigned long long test_rdtsc() {
// LLVM: call i64 @llvm.x86.rdtsc
}

unsigned int test_mm_getcsr() {
// CIR-LABEL: test_mm_getcsr
// LLVM-LABEL: test_mm_getcsr
return _mm_getcsr();
// 2 allocas: 1. for the return value, 1. for the csr result
// CIR: %{{.*}} = cir.alloca !u32i, !cir.ptr<!u32i>, ["__retval"] {alignment = 4 : i64}
// CIR: %{{.*}} = cir.alloca !s32i, !cir.ptr<!s32i>, ["csrRes"] {alignment = 4 : i64}
// CIR: {{%.*}} = cir.llvm.intrinsic "x86.sse.stmxcsr" {{%.*}} : (!cir.ptr<!s32i>) -> !void
// LLVM: call void @llvm.x86.sse.stmxcsr(ptr {{%.*}})
}

unsigned int test__builtin_ia32_stmxcsr() {
// CIR-LABEL: test__builtin_ia32_stmxcsr
// LLVM-LABEL: test__builtin_ia32_stmxcsr
return __builtin_ia32_stmxcsr();
// CIR: %{{.*}} = cir.alloca !u32i, !cir.ptr<!u32i>, ["__retval"] {alignment = 4 : i64}
// CIR: %{{.*}} = cir.alloca !s32i, !cir.ptr<!s32i>, ["csrRes"] {alignment = 4 : i64}
// CIR: {{%.*}} = cir.llvm.intrinsic "x86.sse.stmxcsr" {{%.*}} : (!cir.ptr<!s32i>) -> !void
// LLVM: call void @llvm.x86.sse.stmxcsr(ptr {{%.*}})
}

void test_mm_setcsr() {
// CIR-LABEL: test_mm_setcsr
// LLVM-LABEL: test_mm_setcsr
_mm_setcsr(0);
// CIR: {{%.*}} = cir.llvm.intrinsic "x86.sse.ldmxcsr" {{%.*}} : (!cir.ptr<!s32i>) -> !void
// LLVM: call void @llvm.x86.sse.ldmxcsr(ptr {{%.*}})
}

void test__builtin_ia32_ldmxcsr() {
// CIR-LABEL: test__builtin_ia32_ldmxcsr
// LLVM-LABEL: test__builtin_ia32_ldmxcsr
__builtin_ia32_ldmxcsr(0);
// CIR: {{%.*}} = cir.llvm.intrinsic "x86.sse.ldmxcsr" {{%.*}} : (!cir.ptr<!s32i>) -> !void
// LLVM: call void @llvm.x86.sse.ldmxcsr(ptr {{%.*}})
}
Loading