Skip to content

Commit

Permalink
[CIR][CIRGen] Implement constant evaluation for integral builtins (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
philnik777 authored and lanza committed Jan 9, 2024
1 parent 1d442f6 commit 0affd8d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
Expr::EvalResult Result;
if (E->isPRValue() && E->EvaluateAsRValue(Result, CGM.getASTContext()) &&
!Result.hasSideEffects()) {
llvm_unreachable("NYI");
if (Result.Val.isInt()) {
return RValue::get(builder.getConstInt(getLoc(E->getSourceRange()),
Result.Val.getInt()));
}
if (Result.Val.isFloat())
llvm_unreachable("NYI");
}

// If current long-double semantics is IEEE 128-bit, replace math builtins
Expand Down
11 changes: 11 additions & 0 deletions clang/test/CIR/CodeGen/builtin-constant-evaluated.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s

auto func() {
return __builtin_strcmp("", "");
// CHECK: cir.func @_Z4funcv() -> !s32i extra( {inline = #cir.inline<no>, optnone = #cir.optnone} ) {
// CHECK-NEXT: %0 = cir.alloca !s32i, cir.ptr <!s32i>, ["__retval"] {alignment = 4 : i64} loc(#loc2)
// CHECK-NEXT: %1 = cir.const(#cir.int<0> : !s32i) : !s32i loc(#loc7)
// CHECK-NEXT: cir.store %1, %0 : !s32i, cir.ptr <!s32i> loc(#loc8)
// CHECK-NEXT: %2 = cir.load %0 : cir.ptr <!s32i>, !s32i loc(#loc8)
// CHECK-NEXT: cir.return %2 : !s32i loc(#loc8)
}

0 comments on commit 0affd8d

Please sign in to comment.