Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751543AbdILQER (ORCPT ); Tue, 12 Sep 2017 12:04:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35608 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751111AbdILQEQ (ORCPT ); Tue, 12 Sep 2017 12:04:16 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D0A84883AD Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=pbonzini@redhat.com Subject: Re: "KVM: x86: generalize guest_cpuid_has_ helpers" breaks clang To: Dmitry Vyukov , =?UTF-8?B?UmFkaW0gS3LEjW3DocWZ?= Cc: David Hildenbrand , LKML , KVM list , llvmlinux@lists.linuxfoundation.org, Alexander Potapenko , andreyknvl , Michael Davidson , Greg Hackmann , Nick Desaulniers References: <20170912151851.GA24313@flask> From: Paolo Bonzini Message-ID: <5900628c-222c-fa71-ae11-00ce611c56dd@redhat.com> Date: Tue, 12 Sep 2017 18:03:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 12 Sep 2017 16:04:16 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2002 Lines: 64 On 12/09/2017 17:54, Dmitry Vyukov wrote: >> I guess clang still eliminates dead branches. Clang optimizer does >> know that these are constant, it just does not allow build >> success/failure nor runtime behavior depend on optimization level and >> compiler version. I.e. with gcc you can get build failure with only >> some compiler flags and/or compiler versions. Clang gives stable >> result. But the optimizer does use constant propagation, etc during >> optimization. I can reproduce it: $ cat f.c int bad_code(); static inline void __attribute__((always_inline)) f(int x) { if (!__builtin_constant_p(x)) bad_code(); } int main() { f(0); f(1); f(100); } $ clang --version clang version 4.0.0 (tags/RELEASE_400/final) $ clang f.c -O2 -c -o f.o $ nm f.o U bad_code 0000000000000000 T main $ gcc f.c -O2 -c -o f.o $ nm f.o 0000000000000000 T main ... but I don't know, it seems very weird. The purpose of __builtin_constant_p is to be resolved only relatively late in the optimization pipeline, and it has been like this for at least 15 years in GCC. The docs say what to expect: You may use this built-in function in either a macro or an inline function. However, if you use it in an inlined function and pass an argument of the function as the argument to the built-in, GCC never returns 1 when you call the inline function with a string constant or compound literal (see Compound Literals) and does not return 1 when you pass a constant numeric value to the inline function **unless you specify the -O option**. (emphasis mine). > I've installed clang-3.9 (the closest version to yours my distribution > gives me) and still got the same error with it. I would expect that > 4.0 should give the same result as well... Are you sure you enabled > KVM/intel/amd? (yes, I know you are maintaining KVM code :)) Heh, I don't know about Radim but "^Rmake" goes straight to "make arch/x86/kvm/" for me. :) Paolo